Saw this issue in the developer console of my android application recently:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kbsbng.androidapps.bankinterestcalculator/com.kbsbng.androidapps.bankinterestcalculator.MainActivity}: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ScrollView$SavedState
It happened because I was using a different layout for a fragment in the layout-w600dp, than the default one. On a device whose width is lesser than 600dp and height is greater than 600dp, a change in orientation would cause a different layout to be loaded. In the layout in layout-w600dp, the fragment id was a ScrollView. In the default layout, the fragment id was a LinearLayout. Hence, when changing from portrait to landscape, in such a device, android would try to cast the LinearLayout to a ScollView, which would fail.
Workaround
I worked around this, by using layout-sw600dp, instead of layout-w600dp. With sw, android matches the device screen width, irrespective of orientation, instead of the available width, which changes based on the orientation. Yes, this means that, in a device with width is lesser than 600dp and height is greater than 600dp, even in landscape mode, I would end up using the layout for portrait mode. I could avoid this by doing one of the following things:
1. Continue to use layout-w600dp, but, then, make sure the fragments are in matching containers, that is, either use the tag for both of them, or have them both in a ScrollView, or a LinearLayout.
2. Use layout-large-land and layout-large-port, and then, again, make sure the fragments are in matching containers, that is, either use the tag for both of them, or have them both in a ScrollView, or a LinearLayout.
For my purpose, using layout-sw600dp was a simpler fix, and given the simple layout I have, the issue of "a device with width is lesser than 600dp and height is greater than 600dp ending up using the layout for portrait mode even in landscape mode" is not a major problem.
External links
A good resource on how to deal with supporting different screen sizes in android: http://developer.android.com/training/multiscreen/screensizes.html
Comments