关于空对象引用的(android.support.v4.widget.DrawerLayout $ DrawerListener)’
你能帮我解决一下这个关于logcat的错误吗? http://pastebin.com/uSXruD54
哪里:
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
在我的家庭课程第79行,这是:
drawer.setDrawerListener(toggle);
http://pastebin.com/cixZ7d9d(MainActivity类,第48行)
我不知道setDrawerListener如何工作抱歉,我在stackoverflow上发现了同样的问题: 如何在v4.DrawerLayout中纠正NullPointerException?
答案说它必须确保我在导航抽屉和布局文件中使用相同的ID,但我没有对导航抽屉做任何更改,因为它是我在Android Studio中选择的活动本身,我刚刚实现它上面的标签。
drawer
是null,大概是因为具有ID drawer_layout
不在app_bar_home
布局中。 你应该只在onCreate()
调用一次setContentView()
onCreate()
。 第二个调用是使用app_bar_home
布局完全替换activity_home
布局。 然后,您的DrawerLayout
不再在Activity
,并且findViewById()
将返回null。
相反, app_bar_home
中的app_bar_home
布局,并删除第二个setContentView()
调用。