2

我有 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 再次准备好?

4

1 回答 1

3

我遇到了类似的问题(我没有使用替换,但我使用了添加),我找到了解决方案。您需要在 onCreateView() 或 onActivityCreated() 中为父片段(片段 A)分离嵌套片段。然后,在对该片段的绑定调用中,您可以重新附加。这是一些示例代码:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    FragmentManager fragmentManager = getChildFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.nested_fragment);
    if (fragment != null) {
        fragmentManager.beginTransaction().detach(fragment).commit();
        fragmentManager.executePendingTransactions();
    }
}

请注意,调用 executePendingTransactions() 很重要。如果我正确理解了问题,这是因为 onActivityCreated() 正在作为 FragmentTransaction 的一部分进行处理。如果您不调用 executePendingTransactions,它将在处理分离调用之前继续嵌套片段。

然后在适配器的绑定调用中,执行以下操作:

public void bindNestedFragment(View view) {
    Fragment fragment = mFragmentManager.findFragmentById(R.id.nested_fragment);
    if (fragment == null) {
        // Create/add fragment here
    } else {
        mFragmentManager.beginTransaction.attach(fragment).commit();
    }
}

您必须通过某种方式让您的适配器访问片段管理器(我只是在构造函数中传递它)。希望这会有所帮助!

于 2015-03-09T23:07:50.387 回答