我有 2 个片段在活动。第一个片段 A 有一个 recyclerview,其中一行包含 viewpager。此 ViewPager 具有使用 FragmentStatePagerAdapter 创建的多个片段(每个页面都是一个片段)。现在单击一个操作项,我将用户导航到片段 B。这里我使用 FragmentTransaction 的 replace() 方法将片段 A 替换为 B。现在按下片段 B 上的后退按钮,应用程序崩溃并出现以下异常:
02-06 18:21:49.038: W/System.err(6916): java.lang.IllegalArgumentException: No view found for id 0x7f110209 (com.example:id/vPager) for fragment VFragment{da512cf #0 id=0x7f110209}
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:939)
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1800)
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-06 18:21:49.038: W/System.err(6916): at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:957)
02-06 18:21:49.039: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1541)
02-06 18:21:49.039: W/System.err(6916): at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:502)
02-06 18:21:49.039: W/System.err(6916): at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:176)
02-06 18:21:49.039: W/System.err(6916): at android.support.v7.app.ActionBarActivity.onBackPressed(ActionBarActivity.java:298)
02-06 18:21:49.039: W/System.err(6916): at com.example.MainActivity.onBackPressed(MainActivity.java:2049)
02-06 18:21:49.039: W/System.err(6916): at android.app.Activity.onKeyUp(Activity.java:2453)
02-06 18:21:49.039: W/System.err(6916): at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
02-06 18:21:49.039: W/System.err(6916): at android.app.Activity.dispatchKeyEvent(Activity.java:2704)
02-06 18:21:49.039: W/System.err(6916): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2221)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3918)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3880)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611)
02-06 18:21:49.039: W/System.err(6916): at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3772)
02-06 18:21:49.039: W/System.err(6916): at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
02-06 18:21:49.039: W/System.err(6916): at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849)
02-06 18:21:49.039: W/System.err(6916): at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840)
02-06 18:21:49.039: W/System.err(6916): at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185)
02-06 18:21:49.039: W/System.err(6916): at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
02-06 18:21:49.039: W/System.err(6916): at android.os.MessageQueue.nativePollOnce(Native Method)
02-06 18:21:49.039: W/System.err(6916): at android.os.MessageQueue.next(MessageQueue.java:143)
02-06 18:21:49.039: W/System.err(6916): at android.os.Looper.loop(Looper.java:122)
02-06 18:21:49.040: W/System.err(6916): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-06 18:21:49.040: W/System.err(6916): at java.lang.reflect.Method.invoke(Native Method)
02-06 18:21:49.040: W/System.err(6916): at java.lang.reflect.Method.invoke(Method.java:372)
02-06 18:21:49.040: W/System.err(6916): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-06 18:21:49.040: W/System.err(6916): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
请注意,如果我使用 FragmentTransaction 的 add() 方法添加片段 B,则后按不会使应用程序崩溃。从上面的崩溃日志看来,甚至在 recyclerview 更新其视图之前,viewpager 子片段正在尝试查找 viewpager。如何防止这种情况或延迟片段状态保留过程,直到 recyclerview 再次准备好?