3

I'm trying to implement a drawer navigation for my app, but I can't even seem to get it started and I can't figure out why. It returns a null pointer exception at the setAdapter, but I don't understand since this is copy and past almost from android and other similar tutorials. Can anyone tell me what I'm doing wrong?

Main Activity

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    private String[] mContentTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContentTitles = getResources().getStringArray(R.array.contentTitles);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mContentTitles));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

DrawerLayout.xml

<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" />
    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:textSize="20sp"
    android:gravity="center_vertical"
    android:paddingStart="14.5sp"
    android:paddingEnd="14.5sp"
    android:minHeight="35sp"
/>

strings.xml

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">FrontRowReady</string>
<string name="action_settings">Settings</string>
<string name="home">Home</string>
<string name="settings">Settings</string>
<string name="inbox">Inbox</string>
<string name="contacts">Contacts</string>

<array name="contentTitles">
    <item>Home</item>
    <item>Inbox</item>
    <item>Contacts</item>
    <item>Settings</item>
</array>

error code

12-23 14:29:11.315: E/AndroidRuntime(30524): FATAL EXCEPTION: main 12-23 14:29:11.315: E/AndroidRuntime(30524): Process: PID: 30524 12-23 14:29:11.315: E/AndroidRuntime(30524): java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.ActivityThread.access$800(ActivityThread.java:135) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.os.Handler.dispatchMessage(Handler.java:102) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.os.Looper.loop(Looper.java:136) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.ActivityThread.main(ActivityThread.java:5017) 12-23 14:29:11.315: E/AndroidRuntime(30524): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 14:29:11.315: E/AndroidRuntime(30524): at java.lang.reflect.Method.invoke(Method.java:515) 12-23 14:29:11.315: E/AndroidRuntime(30524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 12-23 14:29:11.315: E/AndroidRuntime(30524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 12-23 14:29:11.315: E/AndroidRuntime(30524): at dalvik.system.NativeStart.main(Native Method) 12-23 14:29:11.315: E/AndroidRuntime(30524): Caused by: java.lang.NullPointerException 12-23 14:29:11.315: E/AndroidRuntime(30524): at com.maxxpotential.frontrowready.MainActivity.onCreate(MainActivity.java:27) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.Activity.performCreate(Activity.java:5231) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 12-23 14:29:11.315: E/AndroidRuntime(30524): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 12-23 14:29:11.315: E/AndroidRuntime(30524): ... 11 more

6
  • Could you put the error log? Commented Dec 23, 2013 at 19:30
  • Yep just realized that, should be up Commented Dec 23, 2013 at 19:31
  • 1
    Change the setContentView(R.layout.activity_main); to setContentView(R.layout.DrawerLayout); Commented Dec 23, 2013 at 19:41
  • perfect, how can I make that the answer? Commented Dec 23, 2013 at 19:44
  • stackoverflow.com/a/20750768/2652124 Commented Dec 23, 2013 at 20:33

4 Answers 4

9

Change this part of the code:

setContentView(R.layout.activity_main); 

to this:

setContentView(R.layout.DrawerLayout);
Sign up to request clarification or add additional context in comments.

Comments

3

I'm a little curious what your content view is:

setContentView(R.layout.activity_main);

What is your R.layout.activity_main? Does it contain your drawer layout? If not,

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

Will cause mDrawerLayout to be null.

Comments

1

You say your XML is called DrawerLayout.xml, yet you set your content as:

setContentView(R.layout.activity_main);

You need to specify your content as the file containing the layout:

setContentView(R.layout.DrawerLayout);

4 Comments

This may be old but it could help me out. If you set your content view to the xml containing your drawer layout. What about the rest of your activity? Do you get all of the referencing out of the way and once you get to the section of your code with the drawer you set it to the drawer_layout.xml?
I'm not sure I follow - if you look at the xml in the question, the DrawerLayout wraps the rest of the regular Activity layout xml. I think that's what you're looking for.
So say I have started a whole layout without a navigation drawer in mind. If I were to add it into my project it would go: <Drawer><MainLayoutWithEverythingINeed/><FrameLayout/><ListView/>
Pretty much. That's all documented in the DrawerLayout documentation. "To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width."
1
navigationView.getHeaderView(0).findViewById(R.id.SOME_ID);

1 Comment

Please add some comments why you post this code line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.