执行doinbackground()时发生java.lang.runtimeexception错误

时间:2014-04-23 05:32:56

标签: android

package com.example.registraionexample;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ContactList extends Activity {
    private ProgressDialog pDialog;
    private static String url = "http://api.androidhive.info/contacts/";
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_GENDER = "gender";
    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";
    private static final String TAG_PHONE_HOME = "home";
    private static final String TAG_PHONE_OFFICE = "office";
    JSONArray contacts = null;
    ArrayList<HashMap<String, String>> contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.contactlist);

        Toast.makeText(getApplicationContext(), "welcome", 100).show();

        new GetContacts().execute();
    }

    public class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(ContactList.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            JSONfunctions jParser = new JSONfunctions();
            String jsonStr = jParser.getJSONfromURL(url);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String email = c.getString(TAG_EMAIL);
                        String address = c.getString(TAG_ADDRESS);
                        String gender = c.getString(TAG_GENDER);

                        // Phone node is JSON Object
                        JSONObject phone = c.getJSONObject(TAG_PHONE);
                        String mobile = phone.getString(TAG_PHONE_MOBILE);
                        String home = phone.getString(TAG_PHONE_HOME);
                        String office = phone.getString(TAG_PHONE_OFFICE);

                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ID, id);
                        contact.put(TAG_NAME, name);
                        contact.put(TAG_EMAIL, email);
                        contact.put(TAG_PHONE_MOBILE, mobile);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog

            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(ContactList.this,
                    contactList, R.layout.list_item, new String[] { TAG_NAME,
                            TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
                            R.id.name, R.id.email, R.id.mobile });
            ListView lv = (ListView) findViewById(R.id.listView1);
            lv.setAdapter(adapter);
        }

    }
}

这是我使用ListViewAsyncTask打印数据的代码,但是当我尝试运行此应用时,它会在Logcat中显示错误:

java.lang.runtimeexception an error occurred while executing doinbackground()

请告诉我哪里出错了?

2 个答案:

答案 0 :(得分:1)

问题是您忘记初始化ArrayList<HashMap<String, String>> contactList;

解决方案:

contactList = new ArrayList<HashMap<String, String>>();

答案 1 :(得分:1)

您尚未初始化ArrayList<HashMap<String, String>> contactList,所以我猜您必须得到nullpointer错误。

请务必在ArrayList中初始化onCreate(),如下所示:

@Override
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contactlist);

    Toast.makeText(getApplicationContext(), "welcome", 100).show();
    contactList=new ArrayList<HashMap<String, String>>();