CoordinatorLayout 中的 NullPointerException onTouchEvent

NullPointerException in CoordinatorLayout onTouchEvent

我的应用程序出现此错误,原因不明。 None 行与我的代码相关,我真的不知道问题出在哪里以及如何解决。

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[=10=]0(DrawerLayout.java:1801)
      at android.support.v4.widget.DrawerLayout$ViewDragCallback.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>

content_home.xml :

<?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 代码似乎有问题。

好像是a bug in Support Librarycom.android.support:design:23.0.0。我找到了解决问题的答案。

1- 更新支持库会自动解决问题。

2- 覆盖 activity 中的 dispatchTouchEvent 方法将解决问题:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    try {
        return super.dispatchTouchEvent(ev);
    } catch (Exception e) {
        return false;
    }
}

3- This answer 也不错 :

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;
                        }
                    });
                }
            }

正如 Milad Faridnia 所说,这确实是 23.0.0 支持库中的一个错误。但是,就我而言,唯一完全解决问题的方法是更新库。

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

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