Issue
onCreateView can't load listView
mDrawerListView = (ListView) getActivity().findViewById(R.id.navigation_list);
in this upper code
mDrawerListView is Null Object
this is 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>
if i don't use LinearLayout and just use only 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"/>
like this xml and use
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
this code is work
how to call listview from fragment linearlayout?
change code like this
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false).findViewById(R.id.navigation_list);
this can call listView but other error
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Solution
Assuming that mDrawerListView is a ListView in the View hierarchy of the Fragment (and not the navigation drawer), the cause of the error is that the View hierarchy of the Fragment has not yet been created.
In the onCreateView() of the Fragment, replace
mDrawerListView = (ListView) getActivity().findViewById(R.id.navigation_list);
with
mDrawerListView = (ListView) rootView.findViewById(R.id.navigation_list);
Further considerations:
If you make use of a ListFragment instead, you can directly get a reference to the ListView by calling getListView() (after onCreateView() returns). You don't even need to inflate a View with this approach, just pass the list row layout in the super() constructor and call setListAdapter().
EDIT:
For the IllegalStateException error, the problem is that you are treating a View as a child, but that View already has a parent. The problem is covered here:
1. ViewPager with a ListView fragment.
2. Fragments - The specified child already has a parent..
Answered By - Yash Sampat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.