为什么这在我的 aSyncTask 中不起作用?我的应用程序崩溃。我正在尝试从 mysql 表中更新我的列表
protected void onPostExecute(String result){
// dismiss dialog
pDialog.dismiss();
runOnUiThread(new Runnable(){
public void run(){
// Add adapter to the list
MenuAdapter adapter = new MenuAdapter(Albums.this, itemList);
ListView list = (ListView)findViewById(R.id.list);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
}
});
}
日志猫:
02-28 18:21:09.013: E/AndroidRuntime(10600): FATAL EXCEPTION: main
02-28 18:21:09.013: E/AndroidRuntime(10600): java.lang.NullPointerException
02-28 18:21:09.013: E/AndroidRuntime(10600): at com.example.whs.Albums$LoadAllItems.onPostExecute(Albums.java:149)
02-28 18:21:09.013: E/AndroidRuntime(10600): at com.example.whs.Albums$LoadAllItems.onPostExecute(Albums.java:1)
02-28 18:21:09.013: E/AndroidRuntime(10600): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-28 18:21:09.013: E/AndroidRuntime(10600): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-28 18:21:09.013: E/AndroidRuntime(10600): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-28 18:21:09.013: E/AndroidRuntime(10600): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 18:21:09.013: E/AndroidRuntime(10600): at android.os.Looper.loop(Looper.java:137)
02-28 18:21:09.013: E/AndroidRuntime(10600): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-28 18:21:09.013: E/AndroidRuntime(10600): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 18:21:09.013: E/AndroidRuntime(10600): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 18:21:09.013: E/AndroidRuntime(10600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-28 18:21:09.013: E/AndroidRuntime(10600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-28 18:21:09.013: E/AndroidRuntime(10600): at dalvik.system.NativeStart.main(Native Method)
这是我从 mysql 表中创建 itemList 的地方。
items = json.getJSONArray(TAG_ITEMS);
// loop through the items
for (int i = 0; items.length() > i; i++){
// Get the item in variable c
JSONObject c = items.getJSONObject(i);
// Store in a variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String username = c.getString(TAG_USERNAME);
if(username == ""){
username = "onbekend";
}
// Create the HashMap
HashMap<String, String> map = new HashMap<String, String>();
// add it
map.put(TAG_ID, id);
map.put(TAG_USERNAME, username);
map.put(TAG_NAME, name);
map.put(THUMBNAIL, "updates");
// to arraylist
itemList.add(map);
}
我应该为列表视图使用什么 ID,我有“@android:id/list”
编辑:这是使用 Albums.this.getListView() 后的最新日志;
02-28 18:35:10.718: E/AndroidRuntime(23103): FATAL EXCEPTION: main
02-28 18:35:10.718: E/AndroidRuntime(23103): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
02-28 18:35:10.718: E/AndroidRuntime(23103): at com.example.whs.MenuAdapter.getView(MenuAdapter.java:58)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.AbsListView.obtainView(AbsListView.java:2461)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.ListView.makeAndAddView(ListView.java:1775)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.ListView.fillDown(ListView.java:678)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.ListView.fillFromTop(ListView.java:739)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.ListView.layoutChildren(ListView.java:1628)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.AbsListView.onLayout(AbsListView.java:2296)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1644)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.onLayout(LinearLayout.java:1428)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.View.layout(View.java:14055)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewGroup.layout(ViewGroup.java:4604)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2000)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1821)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1118)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4550)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.Choreographer.doFrame(Choreographer.java:525)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.os.Handler.handleCallback(Handler.java:615)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.os.Looper.loop(Looper.java:137)
02-28 18:35:10.718: E/AndroidRuntime(23103): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-28 18:35:10.718: E/AndroidRuntime(23103): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 18:35:10.718: E/AndroidRuntime(23103): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 18:35:10.718: E/AndroidRuntime(23103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-28 18:35:10.718: E/AndroidRuntime(23103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-28 18:35:10.718: E/AndroidRuntime(23103): at dalvik.system.NativeStart.main(Native Method)