在android中的asynctask中的NUll指针异常

时间:2014-07-12 08:28:48

标签: android json

我正在制作一个应用程序,我想在地图上显示手机位置 我在sendin latitude成功服务器,但是当我试图从服务器获取数据时我得到了当我将它存储在hashmap中时它显示空指针异常 我的代码是

package com.example.gpstracking;

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.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Supervisornew extends Activity {
    EditText et;
    Button bt;
    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://dyandroidapps.netii.net/android_db/dy.php";

    // JSON Node names
    private static final String TAG_CONTACTS = "products";
    private static final String TAG_LAT = "lat";
    private static final String TAG_LNG = "lng";

    // contacts JSONArray
    // JSONArray contacts = null;
    JSONArray contacts = null;
    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.supervisor);
        et = (EditText) findViewById(R.id.et_supervisor);
        bt = (Button) findViewById(R.id.btn_supervisor);

    }

    public void track(View v) {
        // new LoadAllProducts().execute();
        new GetContacts().execute();
        // new RetrieveTask().execute();
    }

    private class GetContacts extends AsyncTask<Void, ArrayList<String>, Void> {

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

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            System.out.print(jsonStr);

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

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

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

                        String lat = c.getString(TAG_LAT);
                        String lng = c.getString(TAG_LNG);
                        Log.d("response", lat);
                        Log.d("response", lng);//here i m getting values in log

                        HashMap<String, String> map=new HashMap<String,String>();
                        map.put("lat",lat);
                        map.put("lng", lng);
                        contactList.add(map);// i am getting nullpointer excetion



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

}
    }
}

并告诉我在onpost执行中如何处理将arraylist转移到其他活动 在此先感谢.........

3 个答案:

答案 0 :(得分:1)

在您的代码中,首先在使用它之前初始化contactList,即。在onCreate()方法中添加此行

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

修改

要将arraylist转移到其他活动,请参阅此How to pass ArrayList HashMap from one activity to another

答案 1 :(得分:1)

您忘记创建contactList的对象。您正在访问其引用NPE的引用。你应该在onCreate()中创建对象,如下所示:

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

答案 2 :(得分:0)

另外两个答案给出了Null Pointer的原因,回答你问题的第二部分,即将ArrayList传递给另一个Activity:

使用putExtra(String, Serializable)传递Intent和getSerializableExtra(String)方法中的值以检索数据。

ArrayList<HashMap<String, String>>从活动A传递到活动B

Intent intent = new Intent(this, B.class);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("key_name", "key_value");
ArrayList<HashMap<String, String>> arl = new ArrayList<HashMap<String, String>>();
arl.add(hm);
intent.putExtra("arraylist", arl);
startActivity(intent);

检索活动B中的数据

ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");

参考:How to send hashmap value to another activity using an intent