40

I've had my application out in the store for a while, but it seems it crashes occasionally according to the crash reports in the Developer Console, saying: java.lang.ClassCastException in android.widget.ProgressBar.onRestoreInstanceState with the stack trace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myapp/com.mycompany.myapp.activity.MyActivity}: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ProgressBar$SavedState
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4503)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ProgressBar$SavedState
at android.widget.ProgressBar.onRestoreInstanceState(ProgressBar.java:1093)
at android.view.View.dispatchRestoreInstanceState(View.java:9975)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2408)
at android.view.View.restoreHierarchyState(View.java:9951)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1611)
at android.app.Activity.onRestoreInstanceState(Activity.java:908)
at android.app.Activity.performRestoreInstanceState(Activity.java:880)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1102)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
... 11 more

The reason for these crashes eludes me, and I can't reproduce it on any of my devices. I don't have a onRestoreInstanceState overridden anywhere either. Can anyone point me in a direction that makes sense?

MrJre
  • 7,082
  • 7
  • 53
  • 66

8 Answers8

85

I've seen similar issues to this before, and it's because you have two id's that share the same name.

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

Double check that your application does not reuse the same ID in two different places

Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
jimmithy
  • 6,360
  • 2
  • 31
  • 29
  • With two different places you mean within all of the views used in the activity? – MrJre Mar 08 '13 at 16:10
  • Yes, chances are that you are duplicating id's in the activity. If you post your xml layout above, I may be able to help diagnose the issue better. – jimmithy Mar 08 '13 at 16:53
  • I don't use XML a lot, I basically only set ids to layout some views in a `RelativeLayout`. I guess i'll just have to make sure no two views have the same Id; I'll come back to this. – MrJre Mar 10 '13 at 13:29
  • 1
    For what its worth, the way I normally solve this is to create a resource file called ids, then for each widget I create in code, assign it a pre-defined id. http://developer.android.com/guide/topics/resources/more-resources.html#Id – jimmithy Mar 21 '13 at 13:32
  • 4
    Also check if the the ID is defined differently in Landscape layout and Portrait layout. – SoloPilot Jan 21 '14 at 18:53
  • @SoloPilot you should make an answer out of your comment. My problem was with an id that was assigned to a ScrollView in landscape and to a LinearLayout in portrait – kingston Apr 17 '15 at 16:51
  • Thanks! thats was very helpfull. Its weird because it didnt crash in the emulator but in the phone.? – superUser May 17 '15 at 03:39
  • Solved my problem. I used the Lint tool in eclipse to find the duplicate ids. – wolfaviators Jul 16 '15 at 19:18
  • Great, thanks! I was going crazy and forgetting the obvious – Redwarp Jan 14 '16 at 10:00
  • Thanks for this! Given that the problem is a duplicate id reference, just how much further from the truth could a `ClassCastException` be? And wouldn't it be nice if Android Studio would flag an error if you declare the same id in multiple layouts. Thank God for S.O. or I'd surely have completely lost my mind by now. – rmirabelle Jun 23 '16 at 22:01
  • You can check the stack trace where the error happens `ProgressBar.onRestoreInstanceState`, place a breakpoint there and read the id in runtime. – Singed Sep 03 '16 at 13:01
9

I had a different issue: In one layout the root was a ScrollView while in the other the root was a RelativeView that contained the ScrollView. All widgets ids were identical and there were no duplicates. On rotate the crash log was:

java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ScrollView$SavedState

Once I matched the layouts (so in both the root was either the RelativeLayout or ScrollView), the rotation worked fine.

Not sure why this would cause a crash so any insights would be appreciated...

bkurzius
  • 4,020
  • 2
  • 34
  • 34
4

In my case my portrait xml has Relativelayout and ScrollView from landscape xml with the same ID.

On my activity class I try to inflate the layout and assign it on ViewGroup

So when screen rotation occur it throws: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ScrollView$SavedState

It's because the ScrollView extends FrameLayout while Relativelayout extends ViewGroup

So i just wrap the landscape xml with Relativelayout and solves the problem

noufalcep
  • 3,446
  • 15
  • 33
  • 51
Orville
  • 518
  • 7
  • 17
3

In my case there were two different views with same id. One of them in the main layout and the other was in another layout included by the main layout as a view. I changed one of the ids.

mDonmez
  • 400
  • 4
  • 14
1

I got this crash messsage on rotation when I had a GridView in two different layouts, one portrait and the other landscape. One was contained in a FrameLayout and the other was not, i.e., it was by itself in the layout file. When I removed the wrapping FrameLayout, everything worked fine. (Note: the GridView was used on the master side of a master/view layout.)

The message did not appear until I upgraded my Gradle settings to this:

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'
    defaultConfig {
        applicationId 'com.example.android.redacted.app'
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }  

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.1'

}

They were previously this:

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'
    defaultConfig {
        applicationId 'com.example.android.redacted.app'
        minSdkVersion 11
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.2'
}

It seems Marshmallow is more unforgiving with this bug.

Jon Teets
  • 61
  • 1
  • 3
1

I needed to use different layouts with same Id. So, the work around is

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    try {
        super.onRestoreInstanceState(savedInstanceState);
    } catch (Exception ignored) {
    }
}

This will save your application from crashing.

super.onRestoreInstanceState(savedInstanceState) may be executed only till the point exception occurs. However, I don't find it useful, at least in my apps, so I can afford to bypass onRestoreInstanceState.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
1

You can use for some cases: isSaveEnabled = false for view with the same id to avoid error during restore state

Andrey Tuzov
  • 143
  • 1
  • 7
0

It happened to me with a <ProgressBar /> with android:id="@+id/progress" - without any id value clash. Guess something to do with an existing view with this predefined value, outside my app. Changing to android:id="@+id/progressBar" or whatever workarounds the problem.