2013-02-25 38 views
4

我有兩個片段是活動的,並在平板電腦上並排顯示。當用戶從fragmentA中的列表視圖中選擇一個項目時,我需要將fragmentA中的一些數據傳遞給fragmentB。嘗試從活動獲取數據到fragmentB時發生問題。我收到錯誤:IlligalStateException:碎片已經激活。將數據傳遞到添加的片段 - 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) 

回答

8

你不能改變的在創建後傳遞給片段的參數。你應該做的僅僅是傳遞數據的方法調用,就像這樣:

topBarFragment.onTrackSelected(trackId); 

,並將其用於您的片段:如何

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

欲瞭解更多信息,看看文檔here處理片段之間的通信。

+0

謝謝!非常感謝!這讓我瘋狂。 – Jakob 2013-02-25 21:19:33

+0

工作很好.... – NagarjunaReddy 2013-05-07 05:28:49