为什么这在我的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);
}
});
}
logcat的:
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);
}
我应该为listview使用什么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)
答案 0 :(得分:4)
由于始终在UI线程上调用onPostExecute,因此无需使用runOnUiThread
来访问或更新onPostExecute中的UI元素。将您的代码更改为:
protected void onPostExecute(String result){
// dismiss dialog
pDialog.dismiss();
// access or update UI here without runOnUiThread
}
答案 1 :(得分:1)
我应该为listview使用什么ID,我有“@android:id / list”
您需要使用findViewById(android.R.id.list)
,因为它位于Android命名空间中,而不像常规ID "@id/list"
,您只需使用findViewById(R.id.list)
。
<强>加成强>
这个新错误可能在您的MenuAdapter的getCount()
方法中,但我知道它在getView()
中崩溃...我怎么知道?让我们一起浏览LogCat:
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at com.example.whs.MenuAdapter.getView(MenuAdapter.java:58)
at android.widget.AbsListView.obtainView(AbsListView.java:2461)
at android.widget.ListView.makeAndAddView(ListView.java:1775)
顶部的行是例外,您尝试读取Array中不存在的值。下一行显示代码运行的最后位置以及抛出错误的位置。 MenuAdapter.java:58
是文件名和行号,奖励它也会告诉您方法名称:getView()
。典型适配器中使用的唯一数组是您传递给构造函数的那个......
答案 2 :(得分:0)
runOnUiThread()
不是多余的吗? onPostExecute()
已在UI线程上运行,您不应该需要它。试着把它拿出去看看会发生什么!
来源:http://developer.android.com/reference/android/os/AsyncTask.html