0

到目前为止,我所做的是在我创建了 listview 的活动上。在选择一个列表项时,它会打开一个包含 3 个选项卡片段的活动。在其中一个选项卡中,我正在创建一个 listfragment 和 detailfragment。该应用程序在一段时间后关闭,并且列表也没有更新。我收到空指针异常。还附加了堆栈跟踪。

public class MyListFragment1 extends ListFragment {


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

        ArrayList<HashMap<String, String>> productsList;

        // url to get all products list
        private static String url_all_patients = "http://192.168.44.208/get_all_patients.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_PRODUCTS = "products";
        private static final String TAG_PATIENT_ID = "patient_id";
        private static final String TAG_PATIENT_NAME = "patient_name";

        JSONArray products = null;

        Context ctx;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ctx = inflater.getContext();//.getApplicationContext();

        new LoadAllPatients().execute();

        return inflater.inflate(R.layout.listfragment1, container, false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        Toast.makeText(
                getActivity(), 
                getListView().getItemAtPosition(position).toString(), 
                Toast.LENGTH_LONG).show();
    }






    class LoadAllPatients extends AsyncTask<String, String, String> {


        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_patients, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Patients: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

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

                        // Storing each json item in variable
                        String id = c.getString(TAG_PATIENT_ID);
                        String name = c.getString(TAG_PATIENT_NAME);

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

                        // adding each child node to HashMap key => value
                        map.put(TAG_PATIENT_ID, id);
                        map.put(TAG_PATIENT_NAME, name);

                        // adding HashList to ArrayList
                        productsList.add(map);

                        ListAdapter adapter = new SimpleAdapter(ctx, productsList,R.layout.list_item,new String[] { TAG_PATIENT_ID,
                                TAG_PATIENT_NAME},
                        new int[] { R.id.pid, R.id.name });

                                          // updating listview
                        setListAdapter(adapter);
                    }
                } else {
                    // no products found
                    // Launch Add New product Activity
                    //Intent i = new Intent(getApplicationContext(),
                     //       NewProductActivity.class);
                    // Closing all previous activities
                    //i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    //startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

    }

}

请参阅堆栈跟踪。

05-31 17:13:34.651: E/AndroidRuntime(20173): FATAL EXCEPTION: AsyncTask #3
05-31 17:13:34.651: E/AndroidRuntime(20173): java.lang.RuntimeException: An error occured while executing doInBackground()
05-31 17:13:34.651: E/AndroidRuntime(20173):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.lang.Thread.run(Thread.java:856)
05-31 17:13:34.651: E/AndroidRuntime(20173): Caused by: java.lang.NullPointerException
05-31 17:13:34.651: E/AndroidRuntime(20173):    at com.example.logincheck.MyListFragment1$LoadAllPatients.doInBackground(MyListFragment1.java:234)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at com.example.logincheck.MyListFragment1$LoadAllPatients.doInBackground(MyListFragment1.java:1)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-31 17:13:34.651: E/AndroidRuntime(20173):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-31 17:13:34.651: E/AndroidRuntime(20173):    ... 5 more

这里第 234 行是“productsList.add(map);”

请帮忙。谢谢

4

1 回答 1

0

正如乔恩在对您的问题的评论中所写的那样,需要初始化 ArrayList。我想这样的事情应该这样做(未测试):

ArrayList<HashMap<String, String>> productsList = new ArrayList<Hashmap<String, String>>();
于 2013-05-31T12:16:11.780 回答