Search code examples
javaandroidlistviewandroid-fragmentsdrawerlayout

NullPointerException when setting listview adapter (DrawerLayout)


I have a fragment layout which is the drawer and looks like this:

fragment_overview.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffe887" />

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="multipleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

And the java class for that fragment:

OverviewFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_overview, container, false);

    mlistOptions = getResources().getStringArray(R.array.list_options);
    mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) getActivity().findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
            R.layout.simple_list_item_1, mlistOptions));

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    return root;
}

With this code in the resources folder:

<string-array name="list_options">
    <item>Ship</item>
    <item>Map</item>
    <item>Mail</item>
    <item>Settings</item>
    <item>Logout</item>
</string-array>

And this as a list item:

simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:textColor="@android:color/black"
android:textStyle="italic">

However when the setAdapter method runs in the java class it generates this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

What could have actually gotten wrong in that method call? It is a fragment so we need getActivity as the first parameter, the layout file is just a reference to a simple file and the optionslist is just an arraylist from the resources folder.. what can be null?

Thanks in advance!


Solution

  • try this, instead of using getActivity use root were you inflate your layout because your component now in root view.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_overview, null);
    
        mlistOptions = getResources().getStringArray(R.array.list_options);
        mDrawerLayout = (DrawerLayout) root.findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) root.findViewById(R.id.left_drawer);
    
        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.simple_list_item_1, mlistOptions));
    
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    
        return root;
    }