2012-04-04 91 views
82

我在第一次實例化片段時使用了片段。但第二次我得到這個例外。我找不到發生錯誤的行嗎?java.lang.IllegalStateException:指定的子項已具有父項

04-04 08:51:54.320: E/AndroidRuntime(29713): FATAL EXCEPTION: main 
    04-04 08:51:54.320: E/AndroidRuntime(29713): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addViewInner(ViewGroup.java:3013) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2902) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2859) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.view.ViewGroup.addView(ViewGroup.java:2839) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.NoSaveStateFrameLayout.wrap(Unknown Source) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.BackStackRecord.run(Unknown Source) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Handler.handleCallback(Handler.java:587) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Handler.dispatchMessage(Handler.java:92) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.os.Looper.loop(Looper.java:132) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at android.app.ActivityThread.main(ActivityThread.java:4126) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at java.lang.reflect.Method.invokeNative(Native Method) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at java.lang.reflect.Method.invoke(Method.java:491) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
    04-04 08:51:54.320: E/AndroidRuntime(29713): at dalvik.system.NativeStart.main(Native Method) 

這裏是當我我的列表片段的一個元素上單擊我做什麼。

// If we are not currently showing a fragment for the new 
// position, we need to create and install a new one. 
RouteSearchFragment df = RouteSearchFragment.newInstance(index); 

// Execute a transaction, replacing any existing fragment 
// with this one inside the frame. 
FragmentTransaction ft = fragmentManager.beginTransaction(); 
ft.replace(R.id.details_full, df); 
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
ft.commit(); 

第一次是好的,我從列表中單擊element2的,它也行;但是當我回到element1時,我得到了這個錯誤。

感謝每一個!

+0

需要調用layoutView.removeAllViews();之前添加視圖佈局 – 2012-04-04 08:05:09

+0

@imrankhan錯了!您可以添加而不刪除全部。最重要的部分是缺少的,因爲沒有定製包在堆棧跟蹤中,我建議你刪除一些代碼並逐步測試... – WarrenFaith 2012-04-04 08:20:56

+0

我添加更多細節,請你能看到這個嗎? – 2012-04-04 09:18:35

回答

25

當您在RouteSearchFragment類中重寫OnCreateView,你有沒有

if(view != null) { 
    return view; 
} 

代碼段?

如果是這樣,刪除return語句應該可以解決您的問題。

可以保持代碼並返回查看,如果你不想重新生成視圖數據,並onDestroyView()方法中,你從其父刪除此觀點像這樣:

@Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     if (view != null) { 
      ViewGroup parent = (ViewGroup) view.getParent(); 
      if (parent != null) { 
       parent.removeAllViews(); 
      } 
     } 
    } 
+5

@Override \t public void onDestroyView(){ \t \t super.onDestroyView(); \t \t ViewGroup parentViewGroup =(ViewGroup)mRoutesSearchViewContainer.getParent(); \t \t如果(空= parentViewGroup!){ \t \t \t parentViewGroup.removeView(mRoutesSearchViewContainer); \t \t} \t \t \t} – 2012-04-04 14:37:03

+0

@Medo:您能否給簡要的解釋這一點。我在logcat中顯示相同的錯誤。正如你給出的建議「刪除return語句應該解決你的問題」,但是我們需要在onCreateView()方法中返回View對象。你能提供代碼片段給你解釋嗎?謝謝。 – 2013-02-07 11:49:13

+0

@克里希納只需像通常一樣膨脹並返回視圖。你可以跳過整個if語句。 – Medo 2013-02-08 15:10:24

1

您正在將View添加到layout中,但該視圖已在另一個layout中。視圖不能位於多個位置。

+0

我在我的問題中增加了更多細節,請你能看到嗎? – 2012-04-04 09:19:06

346

對不起張貼到一個老問題,但我能夠使用完全不同的解決方案來修復它。我得到這個例外,但我改變了我的onCreatView覆蓋從這個第一行:

View result = inflater.inflate(R.layout.customer_layout, container); 

...這樣的:

View result = inflater.inflate(R.layout.customer_layout, container, false); 

我不知道爲什麼,但使用接受倍率布爾值作爲第三個參數修復它。我認爲它告訴片段和/或活動不要使用「容器」作爲新創建的視圖的父級。 HTH!

+47

不要對不起 - 這對我和很多其他人都非常有用。謝謝! – sleeke 2013-06-05 23:28:45

+0

也有同樣的問題。它對我很好。謝謝。 – rootpanthera 2013-06-17 13:17:26

+0

非常感謝,我一直忘記這一點,並回到這個答案 – noloman 2016-07-13 14:06:00

39

我很多時候都面臨這個問題。 請添加以下代碼爲解決此問題:

@Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     if (view != null) { 
      ViewGroup parentViewGroup = (ViewGroup) view.getParent(); 
      if (parentViewGroup != null) { 
       parentViewGroup.removeAllViews(); 
      } 
     } 
    } 

感謝

+0

謝謝你在這個解決方案:)我有一個非常具體的情況下,我不能使用inflater來解決這個問題。 你爲我節省了很多時間 – Bojan 2013-06-06 13:12:05

+0

對不起,我不太明白因爲我是新來的機器人。 – 2014-09-05 08:03:58

+0

In view.getParent();劑量查看對象來自哪裏? – 2014-09-05 08:04:44

19

我曾在一個片段此代碼,它崩潰,如果我嘗試回到這個片段

if (mRootView == null) { 
    mRootView = inflater.inflate(R.layout.fragment_main, container, false); 
} 

後在這個線程上收集答案,我意識到mRootView的父項仍然具有mRootView作爲子項。所以,這是我的修復。

if (mRootView == null) { 
    mRootView = inflater.inflate(R.layout.fragment_main, container, false); 
} else { 
    ((ViewGroup) mRootView.getParent()).removeView(mRootView); 
} 

希望這有助於

+1

+1幫助了我。尤其如果您的活動保持不變,但片段管理器在頂部添加新片段。當它回到第一個片段時,需要確保舊的根視圖被刪除。謝謝! – Bundeeteddee 2013-11-20 08:18:35

+0

它幫了我很多。我已經找到了3天的解決方案。簡單而美麗的答案。 – 2014-09-05 08:31:42

+0

哇!終於它爲我工作! 克拉解決方案。謝謝! – avisper 2017-07-05 08:23:10

0

該解決方案可幫助:

public class FragmentItem extends Android.Support.V4.App.Fragment 
{ 
    View rootView; 
    TextView textView; 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if (rootView != null) 
     { 
      ViewGroup parent = (ViewGroup)rootView.Parent; 
      parent.RemoveView(rootView); 
     } else { 
      rootView = inflater.Inflate(Resource.Layout.FragmentItem, container, false); 
      textView = rootView.FindViewById<TextView>(Resource.Id.textViewDisplay);    
     } 
     return rootView; 
    } 
} 
+1

我認爲rootView將永遠是空的,因爲你沒有初始化它。 – 2014-09-05 08:11:56

+0

rootView始終爲空 – Thinsky 2015-10-19 03:34:29

30

如果你有這個說法..

View view = inflater.inflate(R.layout.fragment1, container);//may be Incorrect 

那就試試這個..添加false作爲第三個參數..也許它可以幫助..

View view = inflater.inflate(R.layout.fragment1, container, false);//correct one 
13

這也恰好當onCreateView()返回的觀點並不認爲被誇大了。

例子:

View rootView = inflater.inflate(R.layout.my_fragment, container, false); 

TextView textView = (TextView) rootView.findViewById(R.id.text_view); 
textView.setText("Some text."); 

return textView; 

修復:

return rootView; 

相反的:

return textView; // or whatever you returned 
+1

這是一個愚蠢的錯誤。偶然發生在我身上:P – Pavan 2015-02-25 07:16:00

+0

謝謝!它幫助了我! – 2015-07-03 10:27:37

4

我有這個問題,並在Java代碼中沒能解決它。問題出在我的xml上。

我試圖一個TextView添加到容器中,但已經包裹的LinearLayout內TextView的。

這是原始的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:orientation="vertical" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 
    
 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 
     android:id="@android:id/text1" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:textAppearance="?android:attr/textAppearanceListItemSmall" 
 
     android:gravity="center_vertical" 
 
     android:paddingLeft="16dp" 
 
     android:paddingRight="16dp" 
 
     android:textColor="#fff" 
 
     android:background="?android:attr/activatedBackgroundIndicator" 
 
     android:minHeight="?android:attr/listPreferredItemHeightSmall"/> 
 

 
</LinearLayout>

現在與LinearLayout中刪除:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 
     android:id="@android:id/text1" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:textAppearance="?android:attr/textAppearanceListItemSmall" 
 
     android:gravity="center_vertical" 
 
     android:paddingLeft="16dp" 
 
     android:paddingRight="16dp" 
 
     android:textColor="#fff" 
 
     android:background="?android:attr/activatedBackgroundIndicator" 
 
     android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

這並沒有看起來對我來說很重要,但它的確有竅門,而且我根本沒有更改我的Java代碼。這一切都在xml中。

相關問題