0

xml レイアウトで ListFragment を検索しようとすると、常に null が返されます。SherlockListFragment で SherlockFragmentActivity を使用しています。コードは次のとおりです。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_custom_layout);

        adapter = new My_Custom_Adapter(this, My_Object, My_Object_2);

    }

 // Create the list fragment and add it as the list content.
    if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
        mListFragment list = new mListFragment();
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

    }
}

public static class mListFragment extends SherlockListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(adapter);
        setListShown(true);
    }

}

これは、xml レイアウトの短縮版です。

<RelativeLayout
   android:id="@+id/top_control_bar">
    <!-- Text Views -->
</RelativeLayout>
<RelativeLayout >
    <!-- Button -->
</RelativeLayout>
<LinearLayout
    android:id="@+id/start_data"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/top_control_bar"
    android:orientation="vertical" >

    <fragment 
    android:name="android.support.v4.app.ListFragment"
    android:id="@android:id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

findFragmentById(android.R.id.content)

常に null を返します。レイアウト内でフラグメントが見つからないのはなぜですか?

4

3 に答える 3

1

The problem that you have is that you declared in the xml the Fragment as a ListFragment.

getSupportFragmentManager().findFragmentById(android.R.id.content) uses Support fragment manager, so you will need a SupportMapFragment

Try this:

<fragment 
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@android:id/content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>
于 2014-01-13T11:55:15.630 に答える
0

すべきではない

android:id="@android:id/content"

なれ

android:id="@+id/content"

?

ドキュメントから、間違った構文を使用しているようです。

また、チェーンされたメソッドが記述どおりに正しく機能するかどうかもわかりません。変化する

 getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(android.R.id.content, list);
transaction.commit();

それでも、あなたがそこで何をしようとしているのか正直わかりません。FragmentTransaction の add メソッドを使用しているとき、コンテナにフラグメントを追加しようとしていることに気づいていますか?

于 2013-06-27T23:09:48.140 に答える