在片段里面的Viewpager和按在后面按钮的崩溃

时间:2014-08-08 17:51:56

标签: android android-fragments android-viewpager android-animation

我在里面有一个Viewpager片段。它运作得很好。

当我为包含Viewpager的片段的开始和退出添加动画时,问题就开始了。

transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);

重现问题非常容易。 需要滑动viewpager并立即按下后退按钮。 例外是:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: xx.xxxxx, PID: 28590
java.lang.IllegalArgumentException: No view found for id 0x7f070076 (xx.xxxxx:id/facts_pager) for fragment FactsSlidePageFragment{435056b8 #7 id=0x7f070076}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
        at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:543)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        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:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

同一个日志的另一部分:

08-08 20:20:26.791 D/TAG﹕ Viewpager onPageSelected() event
08-08 20:20:27.061 D/TAG﹕ Fragment on pause called!
08-08 20:20:27.331 D/TAG﹕ FragmentStatePagerAdapter getItem() called. position = 2
08-08 20:20:27.331 E/FragmentManager﹕ No view found for id 0x7f070076     (xx.xxxxx:id/facts_pager) for fragment FactsSlidePageFragment{435056b8 #7 id=0x7f070076}

据我在Log的最后一部分所理解,当我滑动ViewPager时,它需要新的幻灯片片段。他在FragmentStatePagerAdapter中询问了它,但是你看到这个调用是在onPause调用片段之后发生的。并且存在问题,因为片段正在关闭,并且viewpager正在尝试做某事并同时填充自己。

如果我删除此片段上的动画,一切正常,并且没有崩溃。 此外,如果我在ViewPager中选择页面后没有快速按下后退按钮也可以。

如果我仍想在此片段的开始和退出时使用动画,如何击败它?

UPD1:

SO上唯一类似的问题:

ViewPager with FragmentPagerAdapter inside Fragment crashes Application when starting Transaction

1 个答案:

答案 0 :(得分:0)

此代码为我解决了这个问题:

在MainActivity onBackPressed()中:

FragmentManager fm = getSupportFragmentManager();

    FragmentBase myFragment = (FragmentBase) fm
            .findFragmentByTag(ParserConstants.FRAGMENT_MAIN);

    if (myFragment instanceof MyFragment) {
        ((MyFragment) myFragment).removeAdapterFromPager();
    }

在MyFragment中:

public void removeAdapterFromPager() { mViewPager.setAdapter(null); }