3

I'm getting this error in my app with no clear reason. None of the lines are related to my code and I really don't know what's the problem and how to fix that.

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)

Here are my layouts:

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>

It seems there is something wrong with CoordinatorLayout code in onTouchEvent method.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78

2 Answers2

3

It seems it's a bug in Support Library com.android.support:design:23.0.0. I found these answers for solving the problem.

1- Updating Support Library will fix the problem automatically.

2- Overriding dispatchTouchEvent method in activity will fix the problem:

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

3- This answer is also nice :

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
  • 9,113
  • 13
  • 65
  • 78
1

It is indeed a bug in the 23.0.0 support library as Milad Faridnia said. However, in my case the only thing that completely solved the issue was updating the library.

His second suggestion of overriding the dispatchTouchEvent mitigated the issue but did not completely eliminate it.

It is worth noting that the crash seems to only happen when you SLOWLY slide the navigation drawer open. In fact, in my case, opening the drawer slowly crashed the app every single time (might be useful to reproduce this bug and isolate it from other potential ones in your app).

albarc
  • 11
  • 1