user742030
user742030

Reputation:

Android SimpleAdaptor NPE

I'm having issues inflating JSON object data into my ListView fragment.

The data is getting pulled from a database and is converted into a JSON array via PHP then to JSON Objects. Everything is working to that point (see my inline Log.i()'s within my code).

I am getting a NPE as follows:

threadid=1: thread exiting with uncaught exception (group=0x40a781f8) FATAL EXCEPTION: main java.lang.NullPointerException at android.widget.SimpleAdapter.(SimpleAdapter.java:85) at com.andaero.app.NavigationListFragment$1.callback(NavigationListFragment.java:97) at com.andaero.app.NavigationListFragment$1.callback(NavigationListFragment.java:1) at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:440) at com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1010) at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:804) at 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) at dalvik.system.NativeStart.main(Native Method)

I'm not sure what I have wrong. Please show corrected methods in your answer so I can better understand and learn from this. 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
    }
}

Upvotes: 0

Views: 610

Answers (2)

user742030
user742030

Reputation:

IFound the answer here

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

Needed to be:

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

Upvotes: 1

Proxy32
Proxy32

Reputation: 811

The answer to your question might be here -> https://stackoverflow.com/a/7493347/1149631 As it seems to be an issue with your implementation of your SimpleAdapter.

Upvotes: 0

Related Questions