0

I'm trying to add a navigation drawer and I'm using the same google code sample.

But i get a null error at ArrayAdapter.

public class HomeActivity extends AbstractBaseActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mSettiltles;

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

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

    // set a custom shadow that overlays the main content when the drawer opens
    //  mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mSettiltles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }
}

I've set my String array in my string values.

 <string-array name="menu_items">
    <item>My Schedule</item>
    <item>TripSummary</item>
    <item>Feedback</item>
</string-array>  

Please help.

Thank you.

Logcat

12-18 06:33:41.690    1486-1486/com.myntra.delivery E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myntra.delivery/com.myntra.delivery.activities.HomeActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.myntra.delivery.activities.HomeActivity.onCreate(HomeActivity.java:49)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

           at android.app.ActivityThread.access$600(ActivityThread.java:141)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5041)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             at dalvik.system.NativeStart.main(Native Method)`

8
  • 1
    Whats the purpose of mTitle = mDrawerTitle = getTitle(); ??? Dec 18, 2013 at 6:51
  • Why are you inflating layout two time in your onCreate method ?
    – GrIsHu
    Dec 18, 2013 at 6:54
  • what does getTitle() contains ?
    – GrIsHu
    Dec 18, 2013 at 6:54
  • U can ignore gettitle for now. The error is in the ArrayAdapter. GrlsHu- That was copy paste error.
    – buggydroid
    Dec 18, 2013 at 6:56
  • 3
    Does you activity_home layout contain a view with id left_drawer?
    – laalto
    Dec 18, 2013 at 7:02

2 Answers 2

1

I suppose line #49 is this one:

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

Therefore, NPE can only origin from mDrawerList being null. So obviously, your layout does not contain a View with id left_drawer.

1

I'm guessing it's the line:

setContentView(R.layout.activity_home);

And you probably put your ListView in another layout like DrawerLayout as implied by the tutorial. Try changing the setContentView to point to the layout where the ListView can be found.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.