0

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!

boking
  • 95
  • 1
  • 10

3 Answers3

0

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;
}
Pradeep Gupta
  • 1,695
  • 1
  • 7
  • 23
  • 2
    While this may answer the OP's question, posting solid code blocks without any explanation serves no educational purpose. Please improve your answer by explaining why this code is a solution or if it is based on OP's what has changed and for what reason. – Marcin Orlowski May 09 '16 at 10:52
  • @boking accept and up-vote answer if it work for you.so other can get help form this answer – Pradeep Gupta May 09 '16 at 11:24
0

you should populate your drawer list in your main activity. Put this code in your activity's onCreate method.

Then in your fragment's onCreateView you shouuld inflate another Layout that will be put in your (main activity's) FrameLayout.

loukaspd
  • 80
  • 6
0

Try replacing your

mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
mDrawerList = (ListView) getActivity().findViewById(R.id.left_drawer);

with

mDrawerLayout = (DrawerLayout) root.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) root.findViewById(R.id.left_drawer);
Max
  • 12,408
  • 4
  • 48
  • 62