Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的任务不工作_Android_Android Asynctask - Fatal编程技术网

Android 我的任务不工作

Android 我的任务不工作,android,android-asynctask,Android,Android Asynctask,我的任务不起作用。对话框不会关闭,列表也不会更新(我想是因为onPostExecute没有被调用) package com.example.whs; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.http.NameValuePair; import org.json.JSONArray; import org.json.JSONException;

我的任务不起作用。对话框不会关闭,列表也不会更新(我想是因为onPostExecute没有被调用)

package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Albums extends ListActivity {
    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    // Array for the list
    ArrayList<HashMap<String, String>> itemList;

    // url to get the items
    private static String url_all_products = "<my url here>";

    // JSON variables
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_ITEMS = "items";
    private static final String TAG_NAME = "name";
    private static final String TAG_ID = "id";
    private static final String TAG_USERNAME = "username";

    //JSON array
    JSONArray items = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_albums);

        // HashMap for the items
        itemList = new ArrayList<HashMap<String, String>>();

        // Loading the items on the background
        new LoadAllItems().execute();

        // Get list-view
        ListView lv = getListView();

        // On item click
        lv.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                //do
            }
        });
    }

    // class for loading all items
    class LoadAllItems extends AsyncTask<String, String, String> {
        // Before 
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog = new ProgressDialog(Albums.this);
            pDialog.setMessage("Albums laden...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        // Get the products     
        @Override
        protected String doInBackground(String... params) {
            // Build Parameters
            List<NameValuePair> params1 = new ArrayList<NameValuePair>();
            //Get the JSON string
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params1);

            // Check for response
            Log.d("All Items: ", json.toString());

            try {
                // check for success tag
                int success = json.getInt(TAG_SUCCESS);

                if(success == 1) {
                    // found the items
                    items = json.getJSONArray(TAG_ITEMS);

                    // loop through the items
                    for (int i = 0; items.length() > i; i++){
                        // Get the item in variable c
                        JSONObject c = items.getJSONObject(i);

                        // Store in a variable
                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String username = c.getString(TAG_USERNAME);
                        if(username == ""){
                            username = "onbekend";
                        }

                        // Create the HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // add it
                        map.put(TAG_ID, id);
                        map.put(TAG_USERNAME, username);
                        map.put(TAG_NAME, name);

                        // to arraylist
                        itemList.add(map);
                    }
                }else{
                    // nothing found
                    Intent i = new Intent(getApplicationContext(), Index.class);
                    // close the previous activities                    
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }       
    }

    protected void onPostExecute(String result){
        // dismiss dialog
        pDialog.dismiss();      
        runOnUiThread(new Runnable(){
            public void run(){
                // Add adapter to the list
                MenuAdapter adapter = new MenuAdapter(Albums.this, itemList);
                ListView list = (ListView)findViewById(R.id.list);
                list.setAdapter(adapter);   
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.albums, menu);
        return true;
    }

}
package com.example.whs;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
公共类相册扩展了ListActivity{
//进度对话框
私人对话;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
//列表的数组
ArrayList项目列表;
//获取项目的url
私有静态字符串url_all_products=“”;
//JSON变量
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
私有静态最终字符串标记_ITEMS=“ITEMS”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记\u USERNAME=“USERNAME”;
//JSON数组
JSONArray items=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u相册);
//项目的HashMap
itemList=新的ArrayList();
//在后台加载项目
新建LoadAllItems().execute();
//获取列表视图
ListView lv=getListView();
//点击项目
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//做
}
});
}
//用于加载所有项目的类
类LoadAllItems扩展了AsyncTask{
//以前
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(Albums.this);
setMessage(“充满相册的…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
//获取产品
@凌驾
受保护的字符串doInBackground(字符串…参数){
//构建参数
List params1=new ArrayList();
//获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url_all_products,“GET”,params1);
//检查是否有响应
Log.d(“所有项:,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//找到物品了吗
items=json.getJSONArray(TAG_items);
//循环浏览项目
对于(int i=0;items.length()>i;i++){
//获取变量c中的项
JSONObject c=items.getJSONObject(i);
//存储在变量中
String id=c.getString(TAG_id);
String name=c.getString(标记名称);
字符串用户名=c.getString(标记\用户名);
如果(用户名==“”){
username=“onbekend”;
}
//创建HashMap
HashMap=newHashMap();
//加上
地图放置(标签标识,标识);
map.put(TAG_用户名、用户名);
地图放置(标签名称、名称);
//排列列表
itemList.add(map);
}
}否则{
//什么也没找到
Intent i=新的Intent(getApplicationContext(),Index.class);
//关闭以前的活动
i、 添加标志(意图、标志、活动、清除、顶部);
星触觉(i);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}       
}
受保护的void onPostExecute(字符串结果){
//取消对话框
pDialog.disclose();
runOnUiThread(新的Runnable(){
公开募捐{
//将适配器添加到列表中
MenuAdapter=新MenuAdapter(Albums.this,itemList);
ListView列表=(ListView)findViewById(R.id.list);
list.setAdapter(适配器);
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.albums,menu);
返回true;
}
}
循环工作正常

如何修复此问题?

1)如果您试图在后台线程中启动活动,请不要执行此操作

2) 您可能会遇到一个json异常,只需记录更多的事情,就可以看到发生了什么


这可能就是为什么它永远不会到达onPostExecute

您不能在doInBackGround()中执行与UI相关的操作,就像您在doInBackGround中分层一个新活动一样

 Intent i = new Intent(getApplicationContext(), Index.class);
                    // close the previous activities                    
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
onCreate()
方法中创建您的
Listview()
。您在
onPostExecute()
之前是否尝试过
@Override
?您可以删除
runOnUiThrea