在执行doinbackground发生了java.lang.RuntimeException错误() [英] java.lang.runtimeexception error occurred while executing doinbackground()

查看:248
本文介绍了在执行doinbackground发生了java.lang.RuntimeException错误()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
        }

    }
}

这是的ListView 使用的AsyncTask 我的code打印数据,但是当我尝试运行此应用程序则显示错误的logcat:

This is my code for Print data in ListView using AsyncTask but when I try to run this app then it show Error in Logcat :

了java.lang.RuntimeException在执行doinbackground出现错误()

请建议我在哪里,在做错误的?

Please suggest me where am doing mistake?

推荐答案

您尚未初始化的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT; contactList ,所以我猜你一定会得到空指针错误。

You have not initialized your ArrayList<HashMap<String, String>> contactList so i guess you must be getting the nullpointer error.

请确保您初始化的ArrayList 的onCreate()如下:

Make sure you initialize your ArrayList in your onCreate() as below:

@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>>();

这篇关于在执行doinbackground发生了java.lang.RuntimeException错误()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆