I am having an issue binding my JSON data Object to a listView fragment. I’m getting an NPE at line 100 where I bind to my Simple Adapter. I have tried various ways about doing this but have been unsuccessful. According to the docs:
You must use ListFragment.setListAdapter() to associate the list with
an adapter. Do not directly call ListView.setAdapter() or else
important initialization will be skipped.
I’m sure that is my issue but I don’t know how to go about correcting my code. Below is the NPE and my activity. Please any help will be appreciated, esp some code example because I’m learning as I go….
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:100)
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)
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
ArrayList<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 list 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);//<--NPE IS HERE
setListAdapter(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;
}
}
});
}
}
Unless you are omitting code you never assign the value of
corcontext.When calling
You are passing in “c” which will always be null.
switch “c” to
getActivity()and the problem should go awayAdditionaly,
You should initialize this once you know you have access to getActivity(). When a fragment is created it will not immediately have access to the activity until attach(..) is called.