4
votes

After upgrading to Design Support Library 23.0.0 and build SDK 23, my application crashes when I try to dismiss a Snackbar:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.MotionEvent.getAction()' on a null object reference
            at android.support.design.widget.AppBarLayout$Behavior.onInterceptTouchEvent(AppBarLayout.java:729)
            at android.support.design.widget.AppBarLayout$Behavior.onInterceptTouchEvent(AppBarLayout.java:629)
            at android.support.design.widget.CoordinatorLayout.performIntercept(CoordinatorLayout.java:357)
            at android.support.design.widget.CoordinatorLayout.onInterceptTouchEvent(CoordinatorLayout.java:409)

I haven't changed anything since the update and I use the only way that I know off for showing a Snackbar:

Snackbar.make(rootView, getString(R.string.error_no_permissions),Snackbar.LENGTH_LONG).show();

There is an issue reported for NPE on CoordinatorLayout that could be related: CoordinatorLayout NullPointerException in onTouchEvent but I still can't find a workaround for my situation. I've tried this possible solution but still no luck...

EDIT: Turns out the solution is (as @NikolaDespotoski suggested) overriding the default AppBarLayout.Behavior

public class AppBarLayoutBehavior extends AppBarLayout.Behavior {

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child,
            MotionEvent ev) {
        return !(parent != null && child != null && ev != null) || super
                .onInterceptTouchEvent(parent, child, ev);
    }
}

...and use it in our AppBarLayout

((CoordinatorLayout.LayoutParams) findViewById(R.id.appbar).getLayoutParams())
                .setBehavior(new AppBarLayoutBehavior());
3
If you look at the ticket which you've linked, you might notice it's marked as "FutureRelease" which means that issue has been fixed, it just hasn't been released yet. Judging from the comment made on Thursday, August 20th "We're looking to get a hotfix out ASAP", hopefully there will be a release soon to fix this problem. Otherwise, it seems like the best course of action may be to go back to api 22, or ignore the issue until the fix is released.Joseph Roque
Looks like it, but I'm afraid the Snackbar issue won't get fixed since the suggested workaround doesn't solve it. Anyway, I guess you're right, it would be best if I go back to design support 22.2.1 for production and build SDK 22, but that's a problem since I started preparing my app for Android M...Bojan Ilievski
Well, you could continue to prepare for 23.0.0 and ignore the error for now until a fix is released, which will hopefully be before Android M is actually released to users. That way you aren't falling behind in that sense. And if it does come out, you'll just have to build for 22Joseph Roque
How about mitigating invalid/null motion events? Inherit AppBarLayout and just avoid streaming null motion events to the super implementation in onInterceptTouchEvent method, until google releases fix for this.Nikola Despotoski
This is what I was looking for. Thanks @NikolaDespotoski Turns out I only needed to override the AppBarLayout.Behavior onInterceptTouchEvent and set it to my AppBarLayout...Bojan Ilievski

3 Answers

3
votes

Another fix for the time being is overriding onInterceptTouchEvent of the AppBarLayout.Behavior and invalid or null MotionEventbeing discarded, just to mitigate the crash in the super implementation.

That would resound as:

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev){
   return ev != null ? super.onInterceptTouchEvent(parent,child, ev) : true;
}

For the MotionEvent that was probably invalid, we won't do anything about it, so we make the inherited Behavior thinks we have handled it.

1
votes

The issue seems to be fixed in Android Support library 23.0.1 (September 2015)

1
votes

I worked around this (in Support library 24 alpha and Android N Preview) by setting the SnackBar's view to reference my content_main.xml's layout ViewGroup rather than my activity_main.xml layout ViewGroup (CoordinatorLayout).

Same thing happened when setting content_main's layout to FrameLayout or CoordinatorLayout.

(Android Studio's template for an Activity WITH a FAB uses android.support.design.widget.CoordinatorLayout as the Layout in Activity_Main and provides a second xml file, content_main, for you to put in your Views, etc. inside a second layout (typically) there in content_main.)

If you're having trouble with the Snackbar crashing when you touch it/move it, you can try changing the View passed into Snackbar.make(layoutViewName...) to your inner-most layout (or to some other View) and see if that resolves it.

Unfortunately, this caused the FAB, which is in activity_main, to not move up as it should have when the SnackBar came into view.

I had to revert back to 23.2.1 and API 23. No workaround was needed then. I was then able to reference the activity_main's CoordinatorLayout as the first argument in SnackBar.make(view...) The FAB moved up as it was supposed to and swiping away the SnackBar did not cause a crash.

EDIT: 23.3.0 and later should be fine, too. My intention in mentioning reverting to (at the time) 23.2.1 was to note that it was NOT working in 24 alpha but WAS working in the 23.x stable version.