Thursday, 8 August 2013

Android simple adaptere that returns a NPE

Android simple adaptere that returns a NPE

I have a bit of a Listview adapter problem. I am glad to say that I have
got my web service part to work but now there seems to be a problem with
my simple adapter in my Listview. I get an NPE when the app returns the
web service data and tries to display it in the listview. I have look on
stackoverflow to see if there is any problem that relates to mine to and I
have found a couple but they don't really quite help in my situation. So I
was just hoping that somebody can see this problem in my situation and
give me a little advice.
here is my logcat:
08-09 00:19:37.415: E/AndroidRuntime(770): FATAL EXCEPTION: main
08-09 00:19:37.415: E/AndroidRuntime(770): java.lang.NullPointerException
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.widget.ListView.setAdapter(ListView.java:462)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.app.ListActivity.setListAdapter(ListActivity.java:265)
08-09 00:19:37.415: E/AndroidRuntime(770): at
com.theproblemsolver.ListView$LoadAllData.onPostExecute(ListView.java:142)
08-09 00:19:37.415: E/AndroidRuntime(770): at
com.theproblemsolver.ListView$LoadAllData.onPostExecute(ListView.java:1)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.os.AsyncTask.finish(AsyncTask.java:631)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.os.AsyncTask.access$600(AsyncTask.java:177)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.os.Looper.loop(Looper.java:137)
08-09 00:19:37.415: E/AndroidRuntime(770): at
android.app.ActivityThread.main(ActivityThread.java:5041)
08-09 00:19:37.415: E/AndroidRuntime(770): at
java.lang.reflect.Method.invokeNative(Native Method)
08-09 00:19:37.415: E/AndroidRuntime(770): at
java.lang.reflect.Method.invoke(Method.java:511)
08-09 00:19:37.415: E/AndroidRuntime(770): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-09 00:19:37.415: E/AndroidRuntime(770): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-09 00:19:37.415: E/AndroidRuntime(770): at
dalvik.system.NativeStart.main(Native Method)
ListView:
public class ListView extends ListActivity {
ArrayList<HashMap<String, String>> questionList;
final String TAG_RESULTS = "results";
final String TAG_QUESTION_SUBJECT = "Subject";
final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
final String TAG_QUESTION = "Question";
final String TAG_QUESTION_CONTENT = "Content";
final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
final String TAG_ANSWERS = "Answers";
final String TAG_ANSWER = "Answer";
final String TAG_ANSWERS_CONTENT = "Content";
JSONArray results;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.listview);
//questionList = new ArrayList<HashMap<String, String>>();
new LoadAllData().execute();
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
startActivity(intent);
finish();
}
}
class LoadAllData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pDialog;
pDialog = new ProgressDialog(ListView.this);
pDialog.setMessage("Loading Data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();
}
protected String doInBackground(String... args) {
try {
Intent in = getIntent();
String searchTerm = in.getStringExtra("TAG_SEARCH");
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "http://example.com/json";
JSONParsser jParser = new JSONParsser();
JSONObject json = jParser.readJSONFeed(URL);
try {
results = json.getJSONArray(TAG_RESULTS);
for(int i = 0; i < results.length(); i++) {
JSONObject r = results.getJSONObject(i);
JSONObject Question =
r.getJSONObject(TAG_QUESTION);
String Subject =
Question.getString(TAG_QUESTION_SUBJECT);
String NumAnswers =
Question.getString(TAG_QUESTION_NUMANSWERS);
String ChosenAnswers=
Question.getString(TAG_QUESTION_CHOSENANSWER);
String Content =
Question.getString(TAG_QUESTION_CONTENT);
//JSONObject Answers =
Question.getJSONObject(TAG_ANSWERS);
//JSONObject Answer =
Answers.getJSONObject(TAG_ANSWER);
//String Content1 =
Answers.getString(TAG_ANSWERS_CONTENT);
questionList = new ArrayList<HashMap<String,
String>>();
HashMap<String, String> map = new
HashMap<String, String>();
map.put(TAG_QUESTION_SUBJECT, Subject);
map.put(TAG_QUESTION_NUMANSWERS, NumAnswers);
questionList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TAG_RESULTS ;
}
@Override
protected void onPostExecute(String file_URL) {
ListAdapter adapter = new SimpleAdapter(getBaseContext(),
questionList,
R.layout.listview,
new String[] { TAG_QUESTION_SUBJECT,
TAG_QUESTION_NUMANSWERS }, new int[] {
R.id.Subject, R.id.NumAnswers });
setListAdapter(adapter);
android.widget.ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View
arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
}}
}
listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/NumAnswers"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ScrollView>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

No comments:

Post a Comment