Unexplainable ClassCastException in android.widget.ProgressBar.onRestoreInstanceState

后端 未结 7 1609
予麋鹿
予麋鹿 2020-12-29 19:21

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.

相关标签:
7条回答
  • 2020-12-29 19:30

    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.

    0 讨论(0)
  • 2020-12-29 19:32

    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

    0 讨论(0)
  • 2020-12-29 19:39

    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

    0 讨论(0)
  • 2020-12-29 19:44

    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.

    0 讨论(0)
  • 2020-12-29 19:45

    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.

    0 讨论(0)
  • 2020-12-29 19:47

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

    0 讨论(0)
提交回复
热议问题