【发布时间】:2015-04-03 14:14:36
【问题描述】:
onCreateView 无法加载 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);
此代码有效
如何从片段线性布局调用listview?
像这样修改代码
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.
【问题讨论】:
-
NullPointerException in Fragment's onCreateView() method 的可能副本 ... 和无数其他人
标签: android android-fragments android-listview android-view android-lifecycle