IllegalStateException:应用程序的PagerAdapter在不调用PagerAdapter#notifyDataSetChanged的情况下更改了适配器的内容

时间:2017-04-21 07:08:17

标签: android illegalstateexception notifydatasetchanged

第一次它会正常工作。但是,当我关闭应用程序并重新打开时,我得到此异常,在关闭应用程序项目计数之前是2.我恢复视图寻呼机状态onRestoreInstanceState

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1, found: 2 Pager id: /pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class ScreenSlidePagerAdapter
           at android.support.v4.view.ViewPager.populate(ViewPager.java:1167)
           at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
           at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
           at android.view.View.layout(View.java:16649)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
           at android.support.percent.PercentRelativeLayout.onLayout(PercentRelativeLayout.java:113)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
           at android.support.percent.PercentRelativeLayout.onLayout(PercentRelativeLayout.java:113)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
           at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
           at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
           at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
           at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
           at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
           at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2715)
           at android.view.View.layout(View.java:16662)
           at android.view.ViewGroup.layout(ViewGroup.java:5447)
           at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2203)
           at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1963)
           at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
           at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6091)
           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
           at android.view.Choreographer.doCallbacks(Choreographer.java:670)
           at android.view.Choreographer.doFrame(Choreographer.java:606)
           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
           at android.os.Handler.handleCallback(Handler.java:739)
           at android.os.Handler.dispatchMessage(Handler.java:95)
           at android.os.Looper.loop(Looper.java:148)
           at android.app.ActivityThread.main(ActivityThread.java:5437)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-21 12:18:53.705 29133-29133/com.grabandgo.consumer E/AndroidRuntime: FATAL EXCEPTION: main
            Process: com.grabandgo.consumer, PID: 29133

代码是

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putParcelable("view", mPager.onSaveInstanceState());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) {
        state = savedInstanceState.getParcelable("view");
        if (state != null)
            mPager.onRestoreInstanceState(state);
    }
}


@Override
protected void onResume() {
    super.onResume();
    if (state == null) {
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), this, data, mPager);
        mPager.setAdapter(mPagerAdapter);
    } else mPagerAdapter.notifyDataSetChanged();
    //sa mPager.setKeepScreenOn(true);
    /*mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
    if (mPagerAdapter != null) {
        mPagerAdapter.notifyDataSetChanged();
        mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
    }*/
}

0 个答案:

没有答案