atm我寫了一個小程序,它與我的web服務器上的MYSQL數據庫進行通信。 我使用json發送查詢,並希望在列表視圖中顯示他們的答案。簡單適配器 - 空指針異常
我將json中的數據存儲在Arraylist中,稍後將它們寫入ListAdapter。
它工作正常,但每次在應用程序的第一次啓動時,我得到一個NullPointerException並且App退出。 如果我再次啓動應用程序,一切正常。
我的部分代碼:
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
String id = c.getString(TAG_POST_ID);
String username = c.getString(TAG_USERNAME);
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_MESSAGE);
String datum = c.getString(TAG_DATUM);
String aktusr = c.getString(TAG_AKTUSR);
String maxusr = c.getString(TAG_MAXUSR);
String gender = c.getString(TAG_GENDER);
String activity = c.getString(TAG_ACTIVITY);
String category = c.getString(TAG_CATEGORY);
String cat = c.getString(TAG_CAT_ID);
int catid = Integer.parseInt(cat);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_POST_ID, id);
map.put(TAG_USERNAME, username);
map.put(TAG_TITLE, title);
map.put(TAG_MESSAGE, content);
map.put(TAG_DATUM, datum);
map.put(TAG_AKTUSR, aktusr);
map.put(TAG_MAXUSR, maxusr);
map.put(TAG_GENDER, gender);
map.put(TAG_ACTIVITY, activity);
map.put(TAG_CATEGORY, category);
//map.put(TAG_CAT, category[catid-1]);
map.put(TAG_PIC_ID, Integer.toString(PIC[catid-1]));
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
//String[] from = {TAG_CAT, TAG_POST_ID, TAG_TITLE, TAG_MESSAGE, TAG_USERNAME};
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_comment, new String[] {TAG_PIC_ID,TAG_CATEGORY, TAG_ACTIVITY, TAG_DATUM, TAG_AKTUSR, TAG_MAXUSR, TAG_GENDER, /*TAG_POST_ID,*/ TAG_TITLE, TAG_MESSAGE,
TAG_USERNAME }, new int[] { R.id.imgrow, R.id.category, R.id.activity/*R.id.id*/ , R.id.datum, R.id.aktusr, R.id.maxusr, R.id.gender, /*R.id.category,*/ R.id.title, R.id.message,
R.id.username });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int intid = (int)id;
Toast.makeText(getApplicationContext(), mCommentList.get(intid).get(TAG_POST_ID).toString(), Toast.LENGTH_SHORT).show();
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ReadComments.this);
pDialog.setMessage("Loading Comments...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
//we will develop this method in version 2
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
//we will develop this method in version 2
updateList();
}
}
09-27 15:14:17.593:E/AndroidRuntime(7876):致命異常:主要 09-27 15:14:17.593:電子/ AndroidRuntime(7876):進程:com.example.login_remote,PID:7876 09-27 15:14:17.593:E/AndroidRuntime(7876):顯示java.lang.NullPointerException 09-27 15:14:17.593:電子/ AndroidRuntime(7876):at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93) 09-27 15:14:17.593:E/AndroidRuntime(7876):at android.widget.ListView.setAdapter(ListView.java :480) 09-27 15:14:17.593:E/AndroidRuntime(7876):at android.app.ListActivity.setListAdapter(ListActivity.java:265) 09-27 15:14:17.593:E/AndroidRuntime(7876):at com.example.login_remote.ReadComments.updateList(ReadComments.java:270) 09-27 15:14:17.593:E/AndroidRuntime(7876):at com.example.login_remote.ReadComments.access $ 2(ReadComments.java:256 ) 09-27 15:14:17.593:E/AndroidRuntime(7876):在com.example.login_remote.ReadComments $ LoadCategorys.onPostExecute(ReadComments.java:413) 09-27 15:14:17.593:E/AndroidRuntime (7876):at com.example.login_remote.ReadComments $ LoadCategorys.onPostExecute(ReadComments.java:1) 09-27 15:14:17.593:E/AndroidRuntime(7876):at android.os.AsyncTask.finish(AsyncTask .java:632) 09-27 15:14:17.593:E/AndroidRuntime(7876):at android.os.AsyncTask.ac塞斯$ 600(AsyncTask.java:177) 09-27 15:14:17.593:E/AndroidRuntime(7876):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645) 09-27 15:14 :17.593:E/AndroidRuntime(7876):在android.os.Handler.dispatchMessage(Handler.java:102) 09-27 15:14:17.593:E/AndroidRuntime(7876):at android.os.Looper.loop (Looper.java:212) 09-27 15:14:17.593:E/AndroidRuntime(7876):在android.app.ActivityThread.main(ActivityThread.java:5151) 09-27 15:14:17.593:電子/ AndroidRuntime(7876):at java.lang.reflect.Method.invokeNative(Native Method) 09-27 15:14:17.593:E/AndroidRuntime(7876):at java.lang.reflect.Method.invoke(Method。 java:515) 09-27 15:14:17.593:E/AndroidRuntime(7876):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 09-27 15:14:17.593:E/AndroidRuntime(7876):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) 09-27 15:14:17.593:E/AndroidRuntime( 7876):在dalvik.system.NativeStart.main(本機方法)
所以看起來,問題是線
09-27 15:14:17.593:E/AndroidRuntime(7876) :在com.example.login_remote.ReadComments.updateList(ReadComments.java:270)
setListAdapter(適配器);
你們誰能看到這個錯誤嗎? 我真的不知道這裏錯了什麼,爲什麼錯誤只顯示在第一次啓動。
THX, 格爾茨AllesFAM
您是否確定在創建適配器時mCommentList不爲空? – Pavlos 2014-09-27 13:38:42