【问题标题】:Fatal Exception AsyncTask doInBackground致命异常 AsyncTask doInBackground
【发布时间】:2014-06-24 19:35:29
【问题描述】:

这是我的主要活动代码,但是当我运行代码时,它说不幸的是程序已停止工作。在 LogCat 中它说:执行 doInBackground() 时发生错误。

我回显了所有接收到的 json 值,所有的都被提取并添加到数组列表中。

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

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

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {


    private static final String URL="http://10.0.3.2/other";
    static String TITLE = "username";
    static String DESCIPTION = "password";



     @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         new Getdata().execute();  
    }



     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);

    } 

     private class  Getdata extends AsyncTask<String, Void, Void>{
         ProgressDialog pDialog;
         JSONParser jsp=new JSONParser();
         JSONObject job;
         ArrayList<HashMap<String, String>> arraylist;
         CustomAdapter adapter;

         @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog=new ProgressDialog(MainActivity.this);
            pDialog.setIndeterminate(false);
            pDialog.setTitle("please wait");
            pDialog.setMessage("Loading");
            pDialog.show();
        }

         @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub

             List<NameValuePair> param = new ArrayList<NameValuePair>();
             param.add(new BasicNameValuePair("tag", "all"));
            job= jsp.getJSONFromUrl(URL, param);

            try{
                 JSONArray jarr= job.getJSONArray("names");

                 for (int i = 0; i < jarr.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        job = jarr.getJSONObject(i);
                        // Retrive JSON Objects
                        map.put("username", job.getString("username"));
                        map.put("password", job.getString("password"));
                        Log.e("jsob", job.toString());
                        // Set the JSON Objects into the array
                        arraylist.add(map);

                    }

               }      catch(JSONException e){
                            e.printStackTrace();
                        }


            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            ListView lv= (ListView) findViewById(R.id.list);
            adapter = new CustomAdapter(MainActivity.this, arraylist);
            lv.setAdapter(adapter);
            pDialog.dismiss();




        }


     }

}

【问题讨论】:

  • 发布整个错误日志

标签: android android-asynctask


【解决方案1】:

你没有初始化arraylist

【讨论】:

    【解决方案2】:

    我知道这并不能直接解决您的问题,但我想我还是应该提一下。使用异步库可能值得研究。

    使用库来帮助处理异步回调可能非常有帮助

    这是我常用的:

    这将处理 GET 和 POST 请求,具有许多很酷的功能,例如自定义超时、JSON 格式、onSuccess() 和 onFailure() 方法等。这个库也有很多工作示例。我已经在我所有的应用程序中使用了它,还没有遇到任何问题!

    希望这会有所帮助。

    【讨论】:

    • 嘿,这很棒,但我没有足够的时间阅读和处理。喜欢小时时间线,所以请如果你能看到它并给我更多建议..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多