I have been busting my head over this for a whole day now, so I figured I would ask for some help. I am trying to get this fragment to properly populate the listView, but no matter what I do, I get a nullpointer exception. I am sure I am missing something really small and I hope someone can point it out for me :)
package com.jaymzzz;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.widget.SearchView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class TestAPICallFragment extends SherlockListFragment implements SearchView.OnQueryTextListener {
ListView listView;
ItemListAdapter itemListAdapter;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onResume() {
super.onResume();
new JSONFromAPI().execute(new String[]{"https://api.jaymzzz.com/api/json"});
}
public void onAttach(Activity activity) {
super.onAttach(activity);
context = this.getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment, container, false);
listView = (ListView) fragmentView.findViewById(android.R.id.list);
return fragmentView;
}
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getActivity(), query, Toast.LENGTH_SHORT);
Log.d("errlog", "Search query = " + query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.d("errlog", "Search query changed to " + newText);
return false; //To change body of implemented methods use File | Settings | File Templates.
}
private class JSONFromAPI extends AsyncTask<String, Void, JSONObject> {
ProgressDialog dialog = new ProgressDialog(getActivity());
protected void onPreExecute() {
dialog.setMessage("Loading...");
dialog.show();
}
protected JSONObject doInBackground(String... urlString) {
String url = urlString[0];
JSONObject json = new utilGetJSONFromAPI().getJSONFromAPI(url);
Log.d("errlog", "Leaving doInBackground");
if (dialog.isShowing()) {
dialog.dismiss();
}
return json;
}
protected void onPostExecute(JSONObject json) {
Log.d("errlog", "Initiating onPostExecute()");
List<ItemEntry> items = new ArrayList<ItemEntry>();
try {
JSONArray message = json.getJSONArray("message");
Log.d("errlog", "Message size: " + message.length());
for (int i = 0; i < message.length(); i++) {
JSONObject e = message.getJSONObject(i);
items.add(new ItemEntry(e.getString("item_name"), e.getString("item_brand_name"), e.getInt("item_id")));
}
} catch (Exception e) {
Log.e("errlog", "Error parsing data " + e.toString());
}
itemListAdapter = new ItemListAdapter(context, R.layout.list_item, R.id.title, R.id.subtitle);
try {
listView.setAdapter(itemListAdapter); // THE LINE THAT KEEPS FAILING
for (final ItemEntry entry : items) {
itemListAdapter.add(entry);
}
} catch (Exception e) {
Toast.makeText(context, "Error Loading data..", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}
Here is the stack trace as well:
12-01 06:33:27.142: WARN/System.err(3003): java.lang.NullPointerException 12-01 06:33:27.142: WARN/System.err(3003): at com.jaymzzz.TestAPICallFragment$JSONFromAPI.onPostExecute(TestAPICallFragment.java:117) 12-01 06:33:27.152: WARN/System.err(3003): at com.jaymzzz.TestAPICallFragment$JSONFromAPI.onPostExecute(TestAPICallFragment.java:72) 12-01 06:33:27.152: WARN/System.err(3003): at >android.os.AsyncTask.finish(AsyncTask.java:602) 12-01 06:33:27.152: WARN/System.err(3003): at android.os.AsyncTask.access$600(AsyncTask.java:156) 12-01 06:33:27.152: WARN/System.err(3003): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 12-01 06:33:27.152: WARN/System.err(3003): at >android.os.Handler.dispatchMessage(Handler.java:99) 12-01 06:33:27.152: WARN/System.err(3003): at android.os.Looper.loop(Looper.java:137) 12-01 06:33:27.152: WARN/System.err(3003): at android.app.ActivityThread.main(ActivityThread.java:4340) 12-01 06:33:27.152: WARN/System.err(3003): at java.lang.reflect.Method.invokeNative(Native >Method) 12-01 06:33:27.162: WARN/System.err(3003): at >java.lang.reflect.Method.invoke(Method.java:511) 12-01 06:33:27.162: WARN/System.err(3003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-01 06:33:27.162: WARN/System.err(3003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-01 06:33:27.162: WARN/System.err(3003): at dalvik.system.NativeStart.main(Native Method)
Help, please :)