0

2 つのフラグメント、ボタンの行、およびリスト フラグメントを使用してアプリケーションを起動しようとしていますが、アプリケーションを起動すると、アプリケーションの onCreateView が呼び出され、getLayoutInflator を呼び出してレイアウト インフレータを取得します。これは次に inflate を呼び出し、ルート レイアウト ファイルの名前に渡されます。このコード内で onCreateView が AGAIN と呼ばれ、無限再帰が発生します。ここにコードがあります

@Override 
public View onCreateView(String name, Context context, AttributeSet attrSet) {
    Log.i(TAG, "onCreateView, name = " + name + " context " + context + " AttributeSet = " + attrSet);
    View v = null; //super.onCreateView(name, context, attrSet);
    try {
        LayoutInflater inflater = getLayoutInflater();
        v = inflater.inflate(R.layout.activity_shop2_drop,null);
        Log.i(TAG,"passed to LayoutInflater: " + R.layout.activity_shop2_drop);
    }
    catch(Exception e) {
        Log.e(TAG, "onCreateView failed: " + e.getMessage());
    }
    return v;
}
4

1 に答える 1

0

ここでの問題は、含まれているビューの ID を渡して Fragment.onCreateView を呼び出したことです。この包含ビューはビュー自体であるため、当然のことながら、コードは私のコードを包含ビューであると考えていたもの、つまりそれ自体にアタッチしようとしました。したがって、無限再帰。別の問題は、inflation コードが activity_shop2_drop.xml で「fragment」タグを検出すると ClassNotFoundException を返し、その結果フラグメントで「onCreateView」を呼び出さないことです。私はまだこれを解決していません。activity_shop2_drop.xml コードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/whole_app_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

    <fragment
        class="com.victorpreston.shop2drop.S2DButtonFragment"
        android:id="@+id/buttonFragmetLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <fragment
        class="com.victorpreston.shop2drop.S2DListFragment"
        android:id="@+id/listFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

ここに、button_fragment_layout.xml コードを示します。

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

    <Button
        android:id="@+id/newItemButton"
        android:layout_width="0dip"
        android:layout_height="40dip"
        android:layout_weight="1"
        android:text="@string/newColumnButton" />

    <Button
        android:id="@+id/newColumnButton"
        android:layout_width="0dip"
        android:layout_height="40dip"
        android:layout_weight="1"
        android:text="@string/newItemButton" />

    <Button
        android:id="@+id/exitButton"
        android:layout_width="0dip"
        android:layout_height="40dip"
        android:layout_weight="1"
        android:text="@string/exitButton" />
    </LinearLayout>

どちらも同じ ID を作成して使用することに注意してください。私はそれを変更しようとしましたが、違いはなかったので、それは問題ではないと思います.

于 2013-03-20T11:50:27.097 に答える