简体   繁体   中英

(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM