Android CoordinatorLayout onTouchEvent中的NullPointerException

Android CoordinatorLayout onTouchEvent中的NullPointerException,android,xml,android-support-library,Android,Xml,Android Support Library,我在应用程序中遇到此错误,原因不明。没有一行与我的代码相关,我真的不知道问题出在哪里,也不知道如何解决 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.afranet.salesportal, PID: 20933 java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX

我在应用程序中遇到此错误,原因不明。没有一行与我的代码相关,我真的不知道问题出在哪里,也不知道如何解决

E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.afranet.salesportal, PID: 20933
      java.lang.NullPointerException: Attempt to invoke virtual method 'float android.view.MotionEvent.getX()' on a null object reference
      at android.view.View.onTouchEvent(View.java:10608)
      at android.support.design.widget.CoordinatorLayout.onTouchEvent(CoordinatorLayout.java:449)
      at android.view.View.dispatchTouchEvent(View.java:9187)
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2644)
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2351)
      at android.support.v4.widget.DrawerLayout.cancelChildViewTouch(DrawerLayout.java:1668)
      at android.support.v4.widget.DrawerLayout$ViewDragCallback.peekDrawer(DrawerLayout.java:1916)
      at android.support.v4.widget.DrawerLayout$ViewDragCallback.access$000(DrawerLayout.java:1801)
      at android.support.v4.widget.DrawerLayout$ViewDragCallback$1.run(DrawerLayout.java:1807)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:145)
      at android.app.ActivityThread.main(ActivityThread.java:6917)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
以下是我的布局:

activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorLightGrayBackground"
    android:layoutDirection="rtl"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorNavbarBackground"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home"
        app:itemIconTint="@drawable/navigation_icon_color"
        app:itemTextColor="@drawable/navigation_icon_color"
        app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".activity.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="" />
        </android.support.v7.widget.Toolbar>


    </android.support.design.widget.AppBarLayout>

    <include
        layout="@layout/content_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?attr/actionBarSize" />

    <!--android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        android:visibility="gone" /  !-->

</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/appbar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activity.HomeActivity"
    tools:showIn="@layout/app_bar_home">


</RelativeLayout>

content_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".activity.HomeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="" />
        </android.support.v7.widget.Toolbar>


    </android.support.design.widget.AppBarLayout>

    <include
        layout="@layout/content_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?attr/actionBarSize" />

    <!--android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"
        android:visibility="gone" /  !-->

</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/appbar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activity.HomeActivity"
    tools:showIn="@layout/app_bar_home">


</RelativeLayout>

onTouchEvent
方法中的
CoordinatorLayout
代码似乎有问题。

似乎是
com.android。支持:设计:23.0.0
。我找到了解决这个问题的答案

1-更新支持库将自动修复该问题

2-重写活动中的
dispatchTouchEvent
方法将解决问题:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    try {
        return super.dispatchTouchEvent(ev);
    } catch (Exception e) {
        return false;
    }
}
3-也很好:

ViewGroup appBarLayout = (ViewGroup) ret.findViewById(R.id.appbarlayout);
            for (int i = 0; i < appBarLayout.getChildCount(); i++) {
                View childView = appBarLayout.getChildAt(i);
                if (!childView.isClickable()) {
                    childView.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View view, MotionEvent motionEvent) {
                            return true;
                        }
                    });
                }
            }
ViewGroup appBarLayout=(ViewGroup)ret.findViewById(R.id.appBarLayout);
对于(int i=0;i
正如Milad Faridnia所说,这确实是23.0.0支持库中的一个bug。然而,在我的例子中,唯一完全解决这个问题的是更新库

他的第二个建议是覆盖
dispatchTouchEvent
,这缓解了问题,但并没有完全消除它


值得注意的是,只有当您缓慢打开导航抽屉时,才会发生崩溃。事实上,在我的例子中,每次打开抽屉都会使应用程序慢慢崩溃(可能有助于重现此错误并将其与应用程序中的其他潜在错误隔离开来)

相关:问题链接:@cricket\u 007谢谢,我想我找到了答案;)哇,第二个答案很好。我挣扎了好几个小时。非常感谢你。