Search code examples
androidandroid-coordinatorlayoutsnackbar

Android Snackbar causes IndexOutOfBoundsException


I'm getting this exception on some devices and i don't know what is causing it.

java.util.ArrayList.throwIndexOutOfBoundsException (ArrayList.java:255)
java.util.ArrayList.get (ArrayList.java:308)
android.support.design.widget.CoordinatorLayout.onChildViewsChanged (CoordinatorLayout.java:1225)  android.support.design.widget.CoordinatorLayout$HierarchyChangeListener.onChildViewRemoved (CoordinatorLayout.java:2863)
android.view.ViewGroup.dispatchViewRemoved (ViewGroup.java:4226)
android.view.ViewGroup.removeViewInternal (ViewGroup.java:4627)
android.view.ViewGroup.removeViewInternal (ViewGroup.java:4582)
android.view.ViewGroup.removeView (ViewGroup.java:4513)
android.support.design.widget.Snackbar.onViewHidden (Snackbar.java:646)
android.support.design.widget.Snackbar.access$300 (Snackbar.java:72)
android.support.design.widget.Snackbar$9.onAnimationEnd (Snackbar.java:588)
android.support.v4.view.ViewPropertyAnimatorCompatJB$1.onAnimationEnd (ViewPropertyAnimatorCompatJB.java:47)
android.view.ViewPropertyAnimator$AnimatorEventListener.onAnimationEnd (ViewPropertyAnimator.java:1114)
android.animation.ValueAnimator.endAnimation (ValueAnimator.java:1239)
android.animation.ValueAnimator$AnimationHandler.doAnimationFrame (ValueAnimator.java:766)
android.animation.ValueAnimator$AnimationHandler$1.run (ValueAnimator.java:801)
android.view.Choreographer$CallbackRecord.run (Choreographer.java:873)
android.view.Choreographer.doCallbacks (Choreographer.java:676)
android.view.Choreographer.doFrame (Choreographer.java:603)
android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:859)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:168)
android.app.ActivityThread.main (ActivityThread.java:5845)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:797)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:687)

I have a activity_main xml file with a include to a app_bar_main.xml which includes a content_mail xml file.

activity_main.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:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        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:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/bar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".application.activities.MainActivity">

    <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"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_main" />

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

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".application.activities.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

I'm making the snackbar on the standard content view, but at almost random moments it crashes because of the Exception. The weird thing is that on my own devices/emulators, i cannot reproduce the errror(altough i was able to reproduce it first, but i changed the Snackbar's view to android.R.id.content and then it stopped appearing on my devices). But on other devices such as a HTC M8(Android 6.0) of a friend of mine the exception does appear.

 Snackbar.make(findViewById(android.R.id.content), "Updating " + info.type, Snackbar.LENGTH_LONG).show();

UPDATE 1: Similiar issues found


Solution

  • I have resolved my issues by downgrading to a older Android Support Library version(v24.1.1). Check out the Android Issue Tracker here.

    UPDATE 1 Seems like it's going to be resolved in de next version of the Android Support Library(v24.2.1)

    UPDATE 2 24.2.1 is out as of today and the error seems to be resolved. At least I couldn't reproduce it with the current version. – PattaFeuFeu