试图在xml布局中查找ListFragment总是返回null。我使用了SherlockFragmentActivity和SherlockListFragment。下面是代码:findFragmentById始终为空
@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。为什么它不能在我的布局中找到片段?
这似乎没有,没有。 – DaveSav
你是什么意思?从[documentation] [1]看来,你似乎使用了错误的语法。 [1]:http://developer.android.com/guide/components/fragments.html#Adding – DigCamara
我将xml更改为您的建议,但遗憾的是没有任何区别 – DaveSav