10

After implementing the new Snackbar in a few of my applications, I am receiving the following error:

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at
android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(SourceFile:1014) at
 android.support.design.widget.SwipeDismissBehavior.onInterceptTouchEvent(SourceFile:188) at
 android.support.design.widget.Snackbar$Behavior.onInterceptTouchEvent(SourceFile:659) at
 android.support.design.widget.Snackbar$Behavior.onInterceptTouchEvent(SourceFile:641) at
 android.support.design.widget.CoordinatorLayout.performIntercept(SourceFile:343) at
 android.support.design.widget.CoordinatorLayout.onInterceptTouchEvent(SourceFile:376) at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1827) at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241) at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946) at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241) at
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946) at
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241) at 
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946) at 
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241) at 
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946) at 
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241) at 
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946) at 
 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2241) at 
 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1946) at 
 com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1968) at 
 com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420) at 
 android.app.Activity.dispatchTouchEvent(Activity.java:2428) at 
 android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(SourceFile:59) at 
 android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(SourceFile:59) at 
 com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1916) at 
 android.view.View.dispatchPointerEvent(View.java:7475) at 
 android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3744) at 
 android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3672) at 
 android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4924) at 
 android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4884) at 
 android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5036) at 
 android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) at 
 android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at 
 android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171) at 
 android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5004) at 
 android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5058) at 
 android.view.Choreographer$CallbackRecord.run(Choreographer.java:776) at 
 android.view.Choreographer.doCallbacks(Choreographer.java:579) at 
 android.view.Choreographer.doFrame(Choreographer.java:546) at 
 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762) at 
 android.os.Handler.handleCallback(Handler.java:725) at 
 android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:153) at 
 android.app.ActivityThread.main(ActivityThread.java:5299) at java.lang.reflect.Method.invokeNative(Native 
 Method) at java.lang.reflect.Method.invoke(Method.java:511) at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at 
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native 
 Method) 

I am using the same setup here: https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/java/com/support/android/designlibdemo/MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Snackbar snackbar;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               // if showing already, close
               if (this.snackbar != null) {
                   this.snackbar.dismiss();
               }

               // make new snackbar
               this.snackbar = Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG).show();


              // chris banes example
              // Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG).setAction("Action", null).show(); 

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        // dismiss when user clicks "home"
        if (this.snackbar != null) {
            this.snackbar.dismiss();
        }
    }
}

Summary:

  • I am using the Snackbar
  • I am using the CoodinartorLayout
  • I am not using the DrawerLayout

Google Issues:

Based on the android.support.v4.widget.ViewDragHelper.shouldInterceptTouchEvent(SourceFile:1014) at line in the Exception, I am guessing it has to do with the "dismissing" of the Snackbar?

Maybe I should not dismiss the Snackbar in onPause? I used to do this with Toasts.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • "I am receiving the following error" -- do you mean that you have not reproduced this in the lab, but you are getting it through crash reporting in production apps? "Maybe I should not dismiss the Snackbar in onPause?" -- well, unlike a `Toast`, AFAIK a `Snackbar` is part of your activity's view hierarchy, so it should not be interfering with other activities that take over the foreground, if that's why you were dismissing it. – CommonsWare Jun 22 '15 at 17:28
  • Yes, exactly. I can't reproduce this on any of my devices or any tests. – Jared Burrows Jun 22 '15 at 17:30
  • Why is this down voted? – Jared Burrows Jun 24 '15 at 15:46
  • @CommonsWare Do you think calling `dismiss` in `onPause` could cause this error? – Jared Burrows Jun 24 '15 at 15:50
  • I have not yet played with their `Snackbar` implementation. My guess is that you don't need to `dismiss()` it (unlike your `Toast` scenario), so I'd try the app without it. Assuming that there are no UX complications, it's possible that it will help with your crash. Or, IOW, removing the `dismiss()` call seems unlikely to make the crashes worse, or more frequent. – CommonsWare Jun 24 '15 at 16:01
  • @CommonsWare That sounds good. The other issue is I can't get this to reproduce. It only shows up in crash reports. I have removed the `dismiss` because it is apart of the `activitiy`'s view. – Jared Burrows Jun 24 '15 at 16:04
  • 1
    Please check [this updated answer](http://stackoverflow.com/a/36004008/2826147) – Amit Vaghela Mar 15 '16 at 06:22

1 Answers1

1

One solution I found was to make sure it was not "swipe-able" by using findViewById(android.R.id.content). This solution basically "avoids" and prevents the error from happening.

Snackbar.make(findViewById(android.R.id.content), "Hello!", Snackbar.LENGTH_LONG);

Should be fixed in 23.2.1: https://stackoverflow.com/a/36004008/950427

Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185