TabView导航期间强制关闭?

时间:2011-12-23 14:10:32

标签: android tabs

在我的Activity中有一个带有3个选项卡的TabView,每个选项卡都有一个ListView和一个Button。 当我使用模拟器上的D-Pad导航选项卡时没有问题,但是当我使用滚轮(鼠标)导航视图然后在视图中单击时有一个ForceClose。我认为它与“touchmodechange”有关,但不知道如何处理这个问题。非常感谢任何帮助...

12-23 19:14:25.352: ERROR/ActivityThread(116): Failed to find provider info for android.server.checkin
12-23 19:18:09.184: ERROR/AndroidRuntime(226): Uncaught handler: thread main exiting due to uncaught exception
12-23 19:18:09.224: ERROR/AndroidRuntime(226): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.widget.ListView.layoutChildren(ListView.java:1428)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:1888)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:591)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.view.ViewRoot.ensureTouchModeLocally(ViewRoot.java:1877)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.view.ViewRoot.ensureTouchMode(ViewRoot.java:1861)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1652)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.os.Looper.loop(Looper.java:123)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at android.app.ActivityThread.main(ActivityThread.java:4203)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at java.lang.reflect.Method.invoke(Method.java:521)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
12-23 19:18:09.224: ERROR/AndroidRuntime(226):     at dalvik.system.NativeStart.main(Native Method)



private void fillData(int tab)
    {
        Cursor cursor;
        Long millis;
        String tempstr;
        //query table and obtain cursor 
        if(tab==0)
            cursor=dbAdapter.getAllRemindersCursor();
        else if(tab==1)
            cursor=dbAdapter.getAutoRemindersCursors();
        else if(tab==2) 
            cursor=dbAdapter.getUserRemindersCursors();
        else
            cursor=dbAdapter.getAllRemindersCursor();

        results.clear();

        //populate the ArrayList using the cursor
        if(cursor!=null)
        {
            if(cursor.moveToFirst())
            {
                Log.d("Msg","Cursor at first item");
                Log.d("Msg",cursor.getCount()+" items in cursor");
                do
                {
                    millis=cursor.getLong(1);
                    tempstr=cursor.getString(2);
                    tempstr=" "+tempstr+"\n"+" "+dtf.format(millis);
                    results.add(tempstr);
                    category.add(cursor.getInt(4));
                    Log.d("Msg","message and type items added");
                }
                while(cursor.moveToNext());
            }
        }
        else
        {
            Log.d("Msg","Cursor is empty");
        }

        if(tab==0)
            lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results));
        else if(tab==1)
            lv2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results));
        else if(tab==2)
            lv3.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results));
        else
            lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results));
    }

每次触发onTabChangedListener时都会调用此方法。其他明智的是 目前此活动中没有其他任何内容。

1 个答案:

答案 0 :(得分:2)

问题是您为所有适配器共享相同的results列表。此列表在适配器内直接引用,因此当您切换选项卡时,您正在更新所有这些选项卡(这就是您获得IllegalStateException的原因)。而不是重用结果变量,只需在filldata方法中创建一个新的,没有必要(我可以从上面的代码中看到)使用相同的。我相信你也可以使用CursorAdapters,而不是打扰中间列表。