gpt4 book ai didi

java - 将数据传递到添加的 fragment - IlligalStateException : Fragment already active

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:16 25 4
gpt4 key购买 nike

我有 2 个 fragment ,它们处于 Activity 状态并在平板电脑上并排显示。当用户从 fragmentA 的 ListView 中选择一个项目时,我需要将一些数据从 fragmentA 传递到 fragmentB。我的问题发生在尝试将数据从 Activity 获取到 fragmentB 时。我收到错误:IlligalStateException: fragment 已激活。

我通过这样的 Activity 传递数据:

    @Override
public void onTrackSelected(String trackId) {

topBarFragment topBarFragment = (topBarFragment)getSupportFragmentManager().findFragmentById(R.id.topBar_fragment);

Bundle bundle = new Bundle();
bundle.putString("trackId", trackId);
//set Fragmentclass Arguments
topBarFragment.setArguments(bundle);

topBarFragment.onTrackSelected();

}

然后像这样在fragmentB中接收:

public void onTrackSelected() {

String trackId = getArguments().getString("trackId");
Toast.makeText(getActivity().getApplicationContext(), trackId,Toast.LENGTH_SHORT).show();

}
}

日志:

02-25 02:58:22.190: E/AndroidRuntime(29593): FATAL EXCEPTION: main
02-25 02:58:22.190: E/AndroidRuntime(29593): java.lang.IllegalStateException: Fragment already active
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.support.v4.app.Fragment.setArguments(Fragment.java:500)
02-25 02:58:22.190: E/AndroidRuntime(29593): at com.harteg.fragmentstest.ItemListActivity.onTrackSelected(ItemListActivity.java:88)
02-25 02:58:22.190: E/AndroidRuntime(29593): at com.harteg.fragmentstest.TracksFragment.onListItemClick(TracksFragment.java:136)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.widget.AbsListView$1.run(AbsListView.java:3423)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.os.Handler.handleCallback(Handler.java:725)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.os.Looper.loop(Looper.java:137)
02-25 02:58:22.190: E/AndroidRuntime(29593): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-25 02:58:22.190: E/AndroidRuntime(29593): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 02:58:22.190: E/AndroidRuntime(29593): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 02:58:22.190: E/AndroidRuntime(29593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-25 02:58:22.190: E/AndroidRuntime(29593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-25 02:58:22.190: E/AndroidRuntime(29593): at dalvik.system.NativeStart.main(Native Method)

最佳答案

fragment 创建后,您无法更改传递给该 fragment 的参数。您应该做的只是在方法调用中传递数据,如下所示:

topBarFragment.onTrackSelected(trackId);

并在你的 fragment 上使用它:

public void onTrackSelected(int trackId) {
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(trackId),Toast.LENGTH_SHORT).show();
}

有关更多信息,请查看文档 here关于如何处理 fragment 之间的通信。

关于java - 将数据传递到添加的 fragment - IlligalStateException : Fragment already active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075280/

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