3

After changing the Height of HorizontalScrollView from

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

to

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

i started getting this crash

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

the crash is happening on this method in HorizontalScrollView

@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();
}

i found a similar issue and i suspected that obfuscation is not keeping Parcelable and they suggested to add this to proguard file

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

when i looked at my proguard file i found that i have already included the below

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

can anyone distinguish the problem and why it started crashing now when i changed the android:layout_height? does this make sense to anyone? thanks in advance

EDIT: after reading some similar problems i found this one java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.support.v7.widget.Toolbar$SavedState
now if you read the answer its saying that using same android:id for different view can cause this so it got my attention that i recently added a view that is included in the same layout with the HorizontalScrollView and that views hold this

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

Can this be the problem?

Mohammad Haidar
  • 1,109
  • 1
  • 16
  • 35

1 Answers1

3

Yes absolutely this is because you are using same id for two layouts.

First, Cross check that your application is not reusing the same ID's in two different places.

The onRestoreInstanceState has performed the findViewById method and the first view to be found was not the HorizontalScrollView.

Ankita
  • 1,129
  • 1
  • 8
  • 15
  • 1
    i will try to reproduce the scenario to make sure that this is the cause – Mohammad Haidar Jul 26 '18 at 09:52
  • @MohammadHaidar Are you able to reproduce the issue? – Girish Jul 17 '19 at 10:34
  • @MohammadHaidar may I know what is the root cause because I am also facing the same. – Girish Jul 18 '19 at 13:08
  • @Girish as it's written in the answer i had views in the same layout with the same ID – Mohammad Haidar Jul 19 '19 at 06:02
  • @MohammadHaidar in that case IDE itself will show "Duplicate id @+id/viewid, already defined earlier in this layout",but this is different. – Girish Jul 19 '19 at 07:23
  • @MohammadHaidar And also even if you have same id in two different layouts for same view say in layout1.xml for horizontalview id is @+id/viewid1 and layout2.xml for horizontalview id is again @+id/viewid1.. in both the layouts view and ids are same..how it will be classcastexception. – Girish Jul 19 '19 at 07:30
  • @Girish i had an view that had inside it a view that holds the same id of a view that exist in the same layout with the included layout, so in this case it won't show syntax error. – Mohammad Haidar Jul 19 '19 at 07:48
  • @MohammadHaidar yeah in above case it won't but I don't have anything like it's two different layout as I explained earlier. – Girish Jul 19 '19 at 11:11