Android Snackbar 导致 IndexOutOfBoundsException

Android Snackbar causes IndexOutOfBoundsException

我在某些设备上遇到此异常,但我不知道是什么原因造成的。

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.access0 (Snackbar.java:72)
android.support.design.widget.Snackbar.onAnimationEnd (Snackbar.java:588)
android.support.v4.view.ViewPropertyAnimatorCompatJB.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.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)

我有一个 activity_main xml 文件,其中包含一个 app_bar_main.xml,其中包含一个 content_mail xml 文件。

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>

我正在标准内容视图上制作快餐栏,但几乎在随机时刻它会因为异常而崩溃。奇怪的是,靠我自己 devices/emulators,我无法重现错误(尽管我能够先重现它,但我将 Snackbar 的视图更改为 android.R.id.content 然后它不再出现在我的设备上) .但是在我朋友的HTC M8(Android 6.0)等其他设备上确实出现了异常。

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

更新 1: 发现类似问题

我已通过降级到较旧的 Android 支持库版本 (v24.1.1) 解决了我的问题。查看 Android 问题跟踪器 here

更新 1 似乎它将在 Android 支持库 (v24.2.1)

的下一个版本中得到解决

更新 2 24.2.1 从今天开始,错误似乎已解决。至少我无法用当前版本重现它。 – PattaFeuFeu