99

フラグメントを最初にインスタンス化するときに、フラグメントを使用しています。しかし、2回目にこの例外が発生しました。エラーが発生した行が見つかりませんでしたか?

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

初めてOKのときは、リストからelement2をクリックします。これもOKです。しかし、element1に戻ると、このバグが発生しました。

みんな、ありがとう!

4

11 に答える 11

369

古い質問に投稿して申し訳ありませんが、まったく別の解決策を使用して修正できました。この例外が発生していましたが、onCreatView オーバーライドの最初の行を次のように変更しました。

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

...これに:

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

理由はわかりませんが、ブール値を3番目のパラメーターとして受け入れるオーバーライドを使用すると修正されました。フラグメントやアクティビティに、新しく作成されたビューの親として「コンテナー」を使用しないように指示していると思います。

于 2012-10-19T00:47:40.613 に答える
36

OnCreateViewあなたのRouteSearchFragmentクラスでオーバーライドするとき、あなたは持っていますか

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();
            }
        }
    }
于 2012-04-04T12:09:36.360 に答える
35

この一言があれば..

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

次に、これを試してください..3番目の引数としてfalseを追加します..役立つかもしれません..

View view = inflater.inflate(R.layout.fragment1, container, false);//correct one
于 2014-07-18T07:52:00.613 に答える
22

このコードはフラグメントにあり、このフラグメントに戻ろうとするとクラッシュしていました

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);
}

お役に立てれば

于 2013-09-04T11:29:51.843 に答える
16

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
于 2014-12-19T20:37:24.803 に答える
5

この問題があり、Java コードで解決できませんでした。問題は私のxmlにありました。

textView をコンテナーに追加しようとしましたが、textView を LinearLayout 内にラップしていました。

これは元の 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にありました。

于 2015-10-13T14:00:38.497 に答える
2

を に追加しようとViewしてlayoutいますが、ビューは既に別の にありlayoutます。ビューを複数の場所に配置することはできません。

于 2012-04-04T08:21:17.203 に答える
1

このソリューションは次のことに役立ちます。

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;
    }
}
于 2014-06-18T06:32:11.520 に答える