关于空对象引用的(android.support.v4.widget.DrawerLayout $ DrawerListener)' [英] (android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference
问题描述
你能帮我解决一下我的logcat上的这个错误吗?
http://pastebin.com/uSXruD54
Can you help me about this error on my 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
$ b我的Home课程第79行的$ b
是:
on my Home class at line 79 which is:
drawer.setDrawerListener(toggle);
http://pastebin.com/cixZ7d9d (MainActivity类,第48行)
http://pastebin.com/cixZ7d9d (MainActivity class, line 48)
我不知道setDrawerListener如何工作抱歉,我找到了stackoverflow上的同样问题:如何纠正v4.DrawerLayout中的NullPointerException?
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?
答案说它必须确保我在导航抽屉和布局文件中使用相同的ID,但我没有对其进行任何更改导航抽屉,因为它是我在Android Studio中选择的活动本身,我只是在其上实现了标签。
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.
推荐答案
抽屉
为空,大概是因为ID drawer_layout
的 DrawerLayout
不在 app_bar_home
布局。您应该只在 onCreate()
中调用 setContentView()
。第二个调用是使用 app_bar_home
布局完全替换 activity_home
布局。您的 DrawerLayout
不再在活动
中,而 findViewById()
将返回null。
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.
相反,< include>
app_bar_home
activity_home
中的布局,并删除第二个 setContentView()
调用。
Instead, <include>
the app_bar_home
layout in activity_home
, and remove the second setContentView()
call.
这篇关于关于空对象引用的(android.support.v4.widget.DrawerLayout $ DrawerListener)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!