0

为什么这在我的 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)
4

3 回答 3

4

因为onPostExecute总是在 UI 线程上调用,所以不需要runOnUiThread在 onPostExecute 中访问或更新 UI 元素。将您的代码更改为:

protected void onPostExecute(String result){
            // dismiss dialog
            pDialog.dismiss();      

            // access or update UI here without runOnUiThread

        }
于 2013-02-28T17:18:51.707 回答
1

我应该为列表视图使用什么 ID,我有“@android:id/list”

您需要使用findViewById(android.R.id.list),因为它位于 Android 命名空间中,与"@id/list"您简单使用的常规 id 不同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)

上面的行是例外,您正在尝试读取数组中不存在的值。下一行显示您的代码运行的最后一个位置以及引发错误的位置。 MenuAdapter.java:58是文件名和行号,另外它也会告诉你方法名:getView(). 典型适配器中使用的唯一数组是您传递给构造函数的数组...

于 2013-02-28T17:29:50.623 回答
0

不是runOnUiThread()多余的吗? onPostExecute()已经在 UI 线程上运行,您应该不需要它。试着把它拿出来看看会发生什么!

来源:http: //developer.android.com/reference/android/os/AsyncTask.html

于 2013-02-28T17:20:34.347 回答