-1

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.
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
user2637015
  • 723
  • 2
  • 12
  • 27
  • possible duplicate of [NullPointerException in Fragment's onCreateView() method](http://stackoverflow.com/questions/15202107/nullpointerexception-in-fragments-oncreateview-method) ... and bazillion others – Selvin Apr 03 '15 at 14:20

2 Answers2

3

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..

Community
  • 1
  • 1
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
0

The below code worked for me.

ListView listView  = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    Log.e("onItemClick", "onItemClick");
                }
            });
thatzprem
  • 4,697
  • 1
  • 33
  • 41