Android SimpleAdaptor NPE

时间:2012-05-29 16:03:51

标签: android json listview hashmap android-inflate

我遇到了将JSON对象数据扩展到ListView片段的问题。

数据从数据库中提取并通过PHP转换为JSON数组,然后转换为JSON对象。一切都在这一点上工作(请参阅我的代码中的内联Log.i())。

我的NPE如下:

  

threadid = 1:线程以未捕获的异常退出(group = 0x40a781f8)   致命异常:主要是java.lang.NullPointerException   android.widget.SimpleAdapter。(SimpleAdapter.java:85)at   com.andaero.app.NavigationListFragment $ 1.callback(NavigationListFragment.java:97)   在   com.andaero.app.NavigationListFragment $ 1.callback(NavigationListFragment.java:1)   在   com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:440)   在   com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1010)   在   com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:804)   在android.os.Handler.handleCallback(Handler.java:605)at   android.os.Handler.dispatchMessage(Handler.java:92)at   android.os.Looper.loop(Looper.java:137)at   android.app.ActivityThread.main(ActivityThread.java:4424)at   java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:511)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:787)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)at   dalvik.system.NativeStart.main(原生方法)

我不确定我的错误。请在答案中显示更正的方法,以便我更好地理解并从中学习。 Thanxs

public class NavigationListFragment extends ListFragment {
    Context context;
    private Activity c;
    final AQuery aq = new AQuery(c);
    private static String url = "http://192.168.1.17/Andaero/php/regulatory_list.php";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.listview, container, false);
        Log.i("NavigationListFragment", "ListView Inflated!!");
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // AsyncTasks.getJSONArrays(context);// asynchronous task for getting
        // JSONarray
        aq.ajax(url, JSONArray.class, new AjaxCallback<JSONArray>() {
            // JSON Node names
            private static final String TAG_ID = "_id";
            private static final String TAG_LABEL = "label";
            private static final String TAG_TITLE = "title";
            private static final String TAG_DISCR = "description";
            private static final String TAG_GO2URL = "gotoURL";

            public void callback(String url, JSONArray json, AjaxStatus status) {
                if (json != null) {
                    // NavigationListFragment.jsonListCallback(json);
                    Log.i("NavigationListFragment", "Caught JSON: " + json.toString());
                    // Hashmap for ListView
                    List<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();
                    try {
                        // Parse the string to a JSON object
                        for (int i = 0; i < json.length(); i++) {
                            JSONObject json_data = json.getJSONObject(i);

                            // Storing each json item in variable
                            String id = json_data.getString(TAG_ID);
                            String label = json_data.getString(TAG_LABEL);
                            String title = json_data.getString(TAG_TITLE);
                            String description = json_data.getString(TAG_DISCR);
                            String gotoURL = json_data.getString(TAG_GO2URL);

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

                            // adding each child node to HashMap key => value
                            map.put(TAG_ID, id);
                            map.put(TAG_LABEL, label);
                            map.put(TAG_TITLE, title);
                            map.put(TAG_DISCR, description);
                            map.put(TAG_GO2URL, gotoURL);

                            // adding HashList to ArrayList
                            mList.add(map);
                            Log.i("NavigationListFragment", "Hash: " + map);
                        }
                    } catch (JSONException e) {
                        Log.e("log_tag", "Error parsing data " + e.toString());
                    }

                    // create the grid item mapping
                    String[] from = new String[] {TAG_LABEL, TAG_TITLE, TAG_DISCR, TAG_GO2URL};
                    int[] to = new int[] { R.id.listLabel, R.id.listTitle, R.id.listDiscription, R.id.dummy };

                    // Updating parsed JSON data into ListView
                    SimpleAdapter adapter = new SimpleAdapter(c, mList, R.layout.list_item, from, to);??<--THIS IS LINE 97 

                    getListView().setAdapter(adapter);

                    // selecting single ListView item
                    ListView lv = getListView();

                    // Launching new screen on Selecting Single ListItem
                    lv.setOnItemClickListener(new OnItemClickListener() {

                        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                            //TODO
                        }
                    });

                }
                // Log any network/JSON Errors
                switch (status.getCode()) {
                    case AjaxStatus.TRANSFORM_ERROR :
                        Log.i("GetJSONArray", "TRANSFORM_ERROR");
                        break;
                    case AjaxStatus.NETWORK_ERROR :
                        Log.i("GetJSONArray", "NETWORK_ERROR");
                        // TODO Create Alert Dialog
                    case AjaxStatus.AUTH_ERROR :
                        Log.i("GetJSONArray", "AUTH_ERROR");
                        break;
                }
            }
        });

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // TODO
    }
}

2 个答案:

答案 0 :(得分:1)

I Found the answer here

SimpleAdapter adapter = new SimpleAdapter(c, mList, R.layout.list_item, from, to);

需要:

SimpleAdapter adapter = new SimpleAdapter(getActivity(), mList, R.layout.list_item, from, to);

答案 1 :(得分:0)

您的问题的答案可能在这里 - &gt; https://stackoverflow.com/a/7493347/1149631 因为您的SimpleAdapter实现似乎是一个问题。