java.lang.IllegalStateException当我在一些空闲时间后设置listview适配器

时间:2015-10-14 12:29:51

标签: android listview android-fragments observer-pattern illegalstateexception

  

一切正常,直到我等待大约5到6分钟,然后再次执行添加特定片段和设置ListView适配器的相同过程,我收到此错误:

 java.lang.IllegalStateException: Observer android.widget.AbsListView$AdapterDataSetObserver@1c5b940e was not registered.
        at android.database.Observable.unregisterObserver(Observable.java:69)
        at android.widget.BaseAdapter.unregisterDataSetObserver(BaseAdapter.java:42)
        at android.widget.AbsListView.onDetachedFromWindow(AbsListView.java:3453)
        at android.view.View.dispatchDetachedFromWindow(View.java:14532)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3133)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3130)
        at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3130)
        at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4497)
        at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4470)
        at android.view.ViewGroup.removeView(ViewGroup.java:4402)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)
        at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1187)
        at android.app.BackStackRecord.run(BackStackRecord.java:798)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:452)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5942)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

在收到此错误之前,我调用了哪些代码:

  btnWith.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!withActive) {
                btnWith.setBackgroundColor(Color.parseColor("#FF6F00"));
                btnDepo.setBackgroundColor(Color.parseColor("#FF8F00"));

                trans = manager.beginTransaction();

                trans.remove(depoFrag);
                trans.add(R.id.placeInMain, withFrag, "Withdrawals Fragment");

                trans.commit();

                depoActive = false;
                withActive = true;

            }
        }

上面的代码位于我的MainActivity.java的onCreate()中,而onAre()又在其onActivityCreated()中调用它

 private void updateTheAdapter() {
    adapter = new SimpleAdapter(getActivity(), postList,
            R.layout.balance_row_withdrawal, new String[]{TAG_AMOUNT, TAG_REASON,
            TAG_DONE_AT_TIME, TAG_DONE_AT_DATE,TAG_ID}, new int[]{R.id.tvListAmount, R.id.tvListReason,
            R.id.tvListTime, R.id.tvListDate,R.id.tvIDWithdraw})};
    }
  

注意:一切正常,直到手机闲置5至6分钟。请帮我解决这个看起来与Observer Pattern有关的错误。

1 个答案:

答案 0 :(得分:0)

每次单击按钮时,您都会调用trans.remove(depoFrag);。如果你同时没有add,那么你试图将它删除两次......这会触发异常吗?