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

时间:2013-02-25 19:52:52

标签: java android android-fragments

我有两个活跃的片段并在平板电脑上并排显示。当用户从fragmentA中的listview中选择一个项目时,我需要将一些数据从fragmentA传递给fragmentB。尝试将活动中的数据传递到fragmentB时出现问题。我收到错误:IlligalStateException:片段已经激活。

我从这样的活动传递数据:

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

    }   
}

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)

1 个答案:

答案 0 :(得分:8)

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

topBarFragment.onTrackSelected(trackId);

并在你的片段上使用它:

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

有关更多信息,请查看文档here,了解如何处理片段之间的通信。