Horizo​​ntalScrollView 在 RestoreInstance 上崩溃

HorizontalScrollView is crashing onRestoreInstance

更改 Horizo​​ntalScrollView 的高度后
<HorizontalScrollView
                android:id="@+id/topics_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="92dp"/>

<HorizontalScrollView
                android:id="@+id/topics_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

我开始遇到这个崩溃

Caused by java.lang.ClassCastException
              android.view.AbsSavedState cannot be cast to android.widget.HorizontalScrollView$SavedState
              android.widget.HorizontalScrollView.onRestoreInstanceState (HorizontalScrollView.java:1678)

Horizo​​ntalScrollView 中的此方法发生崩溃

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (mContext.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        // Some old apps reused IDs in ways they shouldn't have.
        // Don't break them, but they don't get scroll state restoration.
        super.onRestoreInstanceState(state);
        return;
    }
    SavedState ss = (SavedState) state;       //*******this line is crashing
    super.onRestoreInstanceState(ss.getSuperState());
    mSavedState = ss;
    requestLayout();
}

我发现了一个类似的问题,我怀疑混淆没有保持 Parcelable,他们建议将其添加到 proguard 文件

-keepclassmembers class * implements android.os.Parcelable {
static ** CREATOR;
}

当我查看我的 proguard 文件时,我发现我已经包含了以下内容

-keepnames class * implements android.os.Parcelable { *; }
-keepclassmembers class * implements android.os.Parcelable { *; }

任何人都可以区分问题以及为什么当我更改 android:layout_height 时它现在开始崩溃吗? 这对任何人都有意义吗? 提前致谢

编辑:在阅读了一些类似的问题后,我发现了这个
现在,如果您阅读答案,它说对不同的视图使用相同的 android:id 会导致这种情况,所以我注意到我最近添加了一个视图,该视图包含在与 Horizo​​ntalScrollView 相同的布局中,并且该视图持有此

<RelativeLayout
            android:id="@+id/topics_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="92dp"/>

这会是问题所在吗?

是的,这绝对是因为您对两个布局使用了相同的 ID。

首先,交叉检查您的应用程序是否在两个不同的地方重复使用相同的 ID。

onRestoreInstanceState执行了findViewById方法,第一个找到的视图不是Horizo​​ntalScrollView。