导航抽屉在打开后抛出空指针

时间:2015-09-21 17:08:41

标签: android navigation-drawer

我在我的活动中设置了一个导航抽屉,一切正常,但打开抽屉后如果在抽屉外面的屏幕上点击我的应用程序崩溃,我得到空指针异常。

记录猫错误

09-21 16:51:07.663: E/InputEventReceiver(809): Exception dispatching input event.
09-21 16:51:07.663: E/MessageQueue-JNI(809): Exception in MessageQueue   callback: handleReceiveCallback 
09-21 16:51:07.785: E/MessageQueue-JNI(809): java.lang.NullPointerException
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:840)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:866)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1817)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.View.dispatchPointerEvent(View.java:7426)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.os.MessageQueue.nativePollOnce(Native Method)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.os.MessageQueue.next(MessageQueue.java:125)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.os.Looper.loop(Looper.java:124)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at android.app.ActivityThread.main(ActivityThread.java:5041)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at java.lang.reflect.Method.invoke(Method.java:511)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-21 16:51:07.785: E/MessageQueue-JNI(809):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/tablet_plain_bg">

<android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

 <ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

<RelativeLayout
     android:layout_width="650dp"
     android:layout_height="500dp"
     android:layout_centerHorizontal="true"
     android:layout_centerVertical="true"
     android:background="@drawable/tablet_login_ux" >



    <Button
        android:id="@+id/btnChemOrder"
        style="@style/LoginButtonStyle"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btngetnovisitresons"
        android:layout_marginTop="18dp"
        android:layout_toLeftOf="@+id/btnSavegetplanning"
        android:onClick="getChemistOrderClick"
        android:text="Get Chemist Orders Data"
        android:textSize="18dp" />

    <Button
        android:id="@+id/btnSaveChemOrder"
        style="@style/LoginButtonStyle"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnChemOrder"
        android:layout_alignBottom="@+id/btnChemOrder"
        android:layout_alignLeft="@+id/btnSavegetnovisitresons"
        android:onClick="SaveChemistOrderClick"
        android:text="Next"
        android:textSize="18dp" />

            <!-- buttons for cleaning app -->

    <Button
        android:id="@+id/btnDelete"
        style="@style/LoginButtonStyle"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnChemOrder"
        android:layout_below="@+id/btnChemOrder"
        android:layout_marginTop="16dp"
        android:onClick="CleanAppDataClick"
        android:text="Clean Application Data"
        android:textSize="18dp" />

    <Button
        android:id="@+id/btnSaveUsedData"
        style="@style/LoginButtonStyle"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnDelete"
        android:layout_alignBottom="@+id/btnDelete"
        android:layout_alignLeft="@+id/btnSaveChemOrder"
        android:onClick="SaveCleanAppClick"
        android:text="Next"
        android:textSize="18dp" />

 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

DrawerLayout应该是您最外层的布局。您的RelativeLayout会进入其中。更改订单,看看是否有帮助。

来自the docs

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>