2011-05-06 22 views
1

我想一個新片段添加到空的FrameLayout與FragmentTransition,我不斷收到以下異常:IllegalStateException異常的片段轉型

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
    at android.view.ViewGroup.addViewInner(ViewGroup.java:2840) 
    at android.view.ViewGroup.addView(ViewGroup.java:2735) 
    at android.view.ViewGroup.addView(ViewGroup.java:2691) 
    at android.view.ViewGroup.addView(ViewGroup.java:2671) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:739) 
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:919) 
    at android.app.BackStackRecord.run(BackStackRecord.java:578) 
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1217) 
    at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:384) 
    at android.someApp.OverviewListFragment.showDetailsForSelectedMessage(OverviewListFragment.java:67) 

調試時我注意到,FrameLayout里正試圖將自身添加作爲自己的孩子,這似乎是造成這種例外。

這裏是我正在嘗試將片段添加到活動layout.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <fragment 
     android:name="android.someApp.OverviewListFragment" 
     android:id="@+id/overview_list_fragment" 
     android:layout_weight="0.3" 
     android:layout_width="0px" 
     android:layout_height="match_parent" /> 

    <FrameLayout 
     android:id="@+id/scenario_details_container" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="?android:attr/detailsElementBackground"/> 

</LinearLayout> 

這裏就是我試圖執行轉變:

private void showDetailsForSelectedMessage() {  

    ScenarioDetailsFragment fragment = ScenarioDetailsFragment.newInstance(selectedMessage); 
    getFragmentManager().beginTransaction() 
     .replace(R.id.scenario_details_container, fragment) 
     .disallowAddToBackStack() //Problem occurs with and without this line 
     .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) 
    .commit(); 
    getFragmentManager().executePendingTransactions(); 
} 

現在我已經打敗了我的頭對付這個問題太久了。有沒有人對這裏發生的事情有任何線索?

+0

我正面臨類似的問題。你知道了嗎?任何幫助,將不勝感激。 – 2014-09-23 14:49:51

回答

0

我曾經有過類似的例外,我不記得它是什麼引起的。但是,嘗試從XML中移除OverviewListFragment並將其更改爲framelayout佔位符。然後動態創建OverviewListFragment並將其添加到onCreate中的framelayout。 希望這有助於。

相關問題