从添加的片段

时间:2018-06-13 10:55:07

标签: android fragment

我崩溃了崩溃:

Fatal Exception: java.lang.IllegalStateException: Fragment no longer exists for key f0: index 3
   at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:907)
   at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:216)
   at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1455)
   at android.view.View.dispatchRestoreInstanceState(View.java:16881)
   at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3470)
   at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3476)
   at android.view.View.restoreHierarchyState(View.java:16859)
   at android.support.v4.app.Fragment.restoreViewState(Fragment.java:415)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
   at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:855)
   at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2593)
   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
   at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
   at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:854)
   at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:797)
   at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:186)
   at com.kinoapp.mvvm.main.MainActivity.onBackPressed(MainActivity.kt:276)
   at android.app.Activity.onKeyUp(Activity.java:2854)
   at android.view.KeyEvent.dispatch(KeyEvent.java:3346)
   at android.app.Activity.dispatchKeyEvent(Activity.java:3155)
   at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
   at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
   at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:407)
   at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4961)
   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4923)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4458)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4511)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4477)
   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4610)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4485)
   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4667)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4458)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4511)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4477)
   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4485)
   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4458)
   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4511)
   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4477)
   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4643)
   at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4812)
   at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:3033)
   at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2569)
   at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2560)
   at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:3010)
   at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
   at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
   at android.os.MessageQueue.next(MessageQueue.java:323)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:6692)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

当用户点击后退箭头时,我使用此代码删除片段。

override fun onBackPressed() {
    val fragmentUp = supportFragmentManager.findFragmentById(R.id.container_up)
    if(fragmentUp != null){
        super.onBackPressed()
    }
}

Galaxy S7出现了这个问题。

Android版本为7.0

也许有人有类似的崩溃。

帮助解决此问题或帮助了解问题的原因。

1 个答案:

答案 0 :(得分:0)

您需要在onBackPressed

中使用以下代码
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {

        getSupportFragmentManager().popBackStack();


    } else {
        super.onBackPressed();
    }