片段声明的目标片段不属于此FragmentManager [英] Fragment declared target fragment that does not belong to this FragmentManager

查看:90
本文介绍了片段声明的目标片段不属于此FragmentManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一个活动A,其中包含一个片段X.在片段X中,EditText项目具有单击事件,该事件会打开片段Y.此片段显示名称列表.我在列表中按一个名称,片段Y关闭,并将选定的名称发送到片段X EditText.这是我编写的代码:

I have made an activity A which has a fragment X in it. In fragment X, EditText item has on click event which opens fragment Y. This fragment displays a list of names. I press a name in the list, fragment Y closes and sends the selected name of to fragment X EditText. Here's the code I wrote:

YFragment y = new YFragment();
y.setTargetFragment(x.class, code);
getActivity().getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.frame, y)
    .addToBackStack(null)
    .commit();

在片段Y中,我有发送数据的代码,但是问题出在上面的这段代码中.如果我注释掉setTargetFragment行,则该代码将可用,但不会使用,因为不会发送数据.如果我运行该应用程序,则会发生此错误:

In fragment Y I have the code to send the data but the problem is in this block of code above. If I comment out the setTargetFragment line the code will work but no use as data will not be sent. If I run the app this error occurs:

java.lang.IllegalStateException:片段y {46d3d31#3 id = 0x7f090069}声明的目标片段x {e2c16#0 id = 0x7f090104android:switcher:2131296516:0}不属于这个FragmentManager!

java.lang.IllegalStateException: Fragment y{46d3d31 #3 id=0x7f090069} declared target fragment x{e2c16 #0 id=0x7f090104 android:switcher:2131296516:0} that does not belong to this FragmentManager!

推荐答案

要使用 setTargetFragment(),请同时使用新的 Fragment 和目标 Fragment 必须托管在相同的 FragmentManager 中.不会发生这种情况的最常见情况是,如果您同时使用 Activity.getSupportFragmentManager() Fragment.getFragmentManager() Fragment.getChildFragmentManager().

To use setTargetFragment(), both the new Fragment and the target Fragment must be hosted within the same FragmentManager. The most common case where this would not happen is if you are using Activity.getSupportFragmentManager() or Fragment.getFragmentManager() alongside Fragment.getChildFragmentManager().

这篇关于片段声明的目标片段不属于此FragmentManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆