1

Can you help me about this error on my logcat? http://pastebin.com/uSXruD54

Where:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference

on my Home class at line 79 which is:

drawer.setDrawerListener(toggle);

http://pastebin.com/cixZ7d9d (MainActivity class, line 48)

I don't have any idea how setDrawerListener works sorry, I found the same problem here on stackoverflow: How to rectify NullPointerException in v4.DrawerLayout?

The answer says that it has to make sure that I'm using the same id for nav drawer and in layout file but I didn't made any changes on nav drawer because it's the activity itself that I chose in Android Studio, I just implemented tabs on it.

Community
  • 1
  • 1
brettbrdls
  • 483
  • 1
  • 6
  • 16

1 Answers1

4

drawer is null, presumably because the DrawerLayout with ID drawer_layout is not in the app_bar_home layout. You should only be calling setContentView() once in onCreate(). The second call is completely replacing the activity_home layout with the app_bar_home layout. Your DrawerLayout is then no longer in the Activity, and the findViewById() will return null.

Instead, <include> the app_bar_home layout in activity_home, and remove the second setContentView() call.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • 1
    Yup I suspected that because of the two setContentView() that I wrote and removing the other one which is: setContentView(R.layout.app_bar_home); And it works now, thank you so much! – brettbrdls Jan 28 '16 at 05:11