gpt4 book ai didi

android - 在后按时,返回到具有 Fragments 的 RecyclerView 会导致 IllegalArgumentException

转载 作者:行者123 更新时间:2023-11-29 17:41:57 25 4
gpt4 key购买 nike

我有 2 个 fragment 在 Activity 中。第一个 fragment A 有一个 recyclerview,其中一行包含 viewpager。此 ViewPager 具有使用 FragmentStatePagerAdapter 创建的多个 fragment (每个页面都是一个 fragment )。现在单击一个操作项,我将用户导航到 fragment B。在这里,我使用 FragmentTransaction 的 replace() 方法将 fragment A 替换为 B。现在按下 fragment 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() 方法添加 fragment B,则后退键不会使应用程序崩溃。从上面的崩溃日志看来,甚至在 recyclerview 更新其 View 之前,viewpager 子 fragment 正在尝试查找 viewpager。如何防止这种情况或延迟 fragment 状态保留过程,直到 recyclerview 再次准备就绪?

最佳答案

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

@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,它将在处理分离调用之前继续处理嵌套 fragment 。

然后在适配器的绑定(bind)调用中,执行如下操作:

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();
}
}

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

关于android - 在后按时,返回到具有 Fragments 的 RecyclerView 会导致 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28366612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com