IllegalStateException:适配器的内容已更改但ListView未收到通知?

时间:2015-10-19 10:43:25

标签: android android-listview android-wifi

我正在使用app,我必须显示可用wifi的自定义列表。为此我得到列表内部覆盖的方法:

public void onScanComplete(List<ScanResult> results) {}

从这里开始,我调用setAdapter。一旦List显示它滚动正常没有任何崩溃,但当我点击任何列表项连接该wifi提供密码 返回列表以下方法称为: -

 private int counter;
@Override
public void onConnectivityChange(final NetworkInfo info) {
         int len = mItems.size();

        for (int index = 0; index < len; index++) {
            WifiListData item = mItems.get(index);
            item.setIsConnected(false); // setting each to not connected
            item.setState(null);
            if (item.getSSID()!=null&&item.getSSID().equals(info.getExtraInfo().replace("\"", ""))) {
                item.setIsConnected(info.isConnected());
                item.setState(getStatusInSimpleEnglish(info.getDetailedState()));
            }
        }
        if (info.isConnected()) {
            mTimerHandler.removeCallbacks(mTimerRunnable);
        }
        if (info.isConnected() && info.getExtraInfo().equals(String.format("\"%s\"", mSelectedSSID))) {
            DebugLogger.d(TAG, "Connected to " + info.getExtraInfo());
            dismissProgressDialog();
            mSelectedSSID = "";
        }

        if (info.getState() == NetworkInfo.State.CONNECTED) {
            Collections.sort(mItems, new RssiComparator());
            putConnectedOnTop();
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                  connectTOSignIn();
                }
            },1000);
        }
        if (info.getState() == NetworkInfo.State.CONNECTING) {
            Collections.sort(mItems, new RssiComparator());
            putConnectedOnTop();
        }
        notifyAdapter();
}

当我开始滚动应用程序崩溃时,堆栈跟踪如下: -

[null]=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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131624395, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1555)
at android.widget.AbsListView.onTouchMove(AbsListView.java:3549)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3424)
at android.view.View.dispatchTouchEvent(View.java:7706)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2069)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1516)
at android.app.Activity.dispatchTouchEvent(Activity.java:2485)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2017)
at android.view.View.dispatchPointerEvent(View.java:7886)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3956)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3835)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3401)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3451)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3420)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3527)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3428)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3584)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3401)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3451)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3420)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3428)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3401)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5604)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5584)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5555)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5684)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)

我在UI线程中发出通知: -

private  void notifyAdapter()  {
   mActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            homeWifiListAdapter.notifyDataSetChanged();
            }
    });
}

非常感谢任何建议,谢谢。

1 个答案:

答案 0 :(得分:-1)

要更新列表视图中的一行,您可以尝试一下 - &gt; answer

这个伎俩是由罗曼盖伊提供的。