简体   繁体   English

将数据传递给添加的片段 - IlligalStateException:片段已处于活动状态

[英]Passing data to added Fragment - IlligalStateException: Fragment already active

I have 2 fragments which is active and showing side by side on a tablet. 我有两个活跃的片段并在平板电脑上并排显示。 I need to pass some data from fragmentA to fragmentB when user selects an item from a listview in fragmentA. 当用户从fragmentA中的listview中选择一个项目时,我需要将一些数据从fragmentA传递给fragmentB。 My problem occurs when trying to get the data from the activity to fragmentB. 尝试将活动中的数据传递到fragmentB时出现问题。 I'm getting the error: IlligalStateException: Fragment already active. 我收到错误:IlligalStateException:片段已经激活。

I pass the data from activity like this: 我从这样的活动传递数据:

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

    }

And then receive in fragmentB like this: 然后在fragmentB中接收如下:

public void onTrackSelected() {

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

    }   
}

Logcat: logcat的:

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)

You can't change the arguments you passed to the fragment after it has been created. 在创建片段后,您无法更改传递给片段的参数。 What you should do is just pass the data in your method call, like this: 你应该做的只是传递方法调用中的数据,如下所示:

topBarFragment.onTrackSelected(trackId);

And use it on your fragment: 并在你的片段上使用它:

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

For more information have a look at the documentation here on how to handle communication between fragments. 有关更多信息,请查看此处有关如何处理片段之间通信的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM