onCreateView无法加载的ListView如何从片段获取listview?
mDrawerListView = (ListView) getActivity().findViewById(R.id.navigation_list);
在该上代码
mDrawerListView是空对象
这是fragment_navation_drawer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:id="@+id/navigation_list"
android:dividerHeight="0dp"/>
</LinearLayout>
如果我不使用的LinearLayout并且只使用ListView
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:id="@+id/navigation_list"
android:dividerHeight="0dp"/>
这样的XML和使用
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
这个代码是工作
如何从片段的LinearLayout调用列表视图?
变化这样的代码
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false).findViewById(R.id.navigation_list);
这可以调用的ListView但其他错误
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
[片段中的onCreateView()方法的NullPointerException(http://stackoverflow.com/questions/15202107/nullpointerexception-in-fragments-oncreateview-method)bazillion的可能重复...等 – Selvin 2015-04-03 14:20:14