سؤال

I'm trying to use a Navagation Drawer in my application and I keep running into NullPointerExceptions and I don't understand why. My current code which is based largely off of this developer link is:

public class MainActivity extends Activity {
private String[] mPages;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;

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

    mPages = getResources().getStringArray(R.array.page_titles);
    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, mPages));

    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, 
    mDrawerLayout,
    R.drawable.ic_drawer, 
    R.string.drawer_open, 
    R.string.drawer_close 
    ) {


        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
        }


        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
        }
    };


    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

and my drawer_layout.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:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

My stacktrace:

08-12 15:55:54.524: E/AndroidRuntime(1409): FATAL EXCEPTION: main
08-12 15:55:54.524: E/AndroidRuntime(1409): java.lang.RuntimeException:     Unable to start activity     ComponentInfo{com.test.android.nav/com.test.android.nav.MainActivity}:     java.lang.NullPointerException
08-12 15:55:54.524: E/AndroidRuntime(1409):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.os.Looper.loop(Looper.java:137)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at java.lang.reflect.Method.invoke(Method.java:511)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at dalvik.system.NativeStart.main(Native Method)
08-12 15:55:54.524: E/AndroidRuntime(1409): Caused by: java.lang.NullPointerException
08-12 15:55:54.524: E/AndroidRuntime(1409):     at com.test.android.nav.MainActivity.onCreate(MainActivity.java:30)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.Activity.performCreate(Activity.java:5008)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-12 15:55:54.524: E/AndroidRuntime(1409):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-12 15:55:54.524: E/AndroidRuntime(1409):     ... 11 more

line 30 is : mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPages)); and I do have that as an xml file in my res folder.

Now, before I put in that line, I was getting a NullPointerException on this line: mDrawerLayout.setDrawerListener(mDrawerToggle);

Any help would be much appreciated!

هل كانت مفيدة؟

المحلول

I see two potential reasons for your NullPointerException:

The first one could be that this line:

mPages = getResources().getStringArray(R.array.page_titles);

does not return a valid array to mPages. I suppose you do not have a string array called "page_titles" in your resources folder.

If your posted code is absolutely correct, one other reason could be this line:

setContentView(R.layout.activity_main);

since according to your code, the layoutfile of the navigationdrawer is drawer_layout.xml. So it should look like this:

setContentView(R.layout.drawer_layout);

In my opinion, the resource id of your DrawerLayout and the name of the .xml file both being "drawer_layout" is very misleading, I would strongly recommend to change that.

Furthermore, I don't see the reason why you are calling this line:

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

twice in your onCreate() method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top