简体   繁体   English

android.widget.ProgressBar.onRestoreInstanceState 中无法解释的 ClassCastException

[英]Unexplainable ClassCastException in android.widget.ProgressBar.onRestoreInstanceState

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.ClassCastException in android.widget.ProgressBar.onRestoreInstanceState和堆栈跟踪:

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.我也没有在任何地方覆盖onRestoreInstanceState Can anyone point me in a direction that makes sense?谁能指出我一个有意义的方向?

I've seen similar issues to this before, and it's because you have two id's that share the same name.我以前见过类似的问题,这是因为您有两个共享相同名称的 ID。

The onRestoreInstanceState has performed the findViewById method and the first view to be found was not the ProgressView . onRestoreInstanceState已经执行了findViewById方法并且要找到的第一个视图不是ProgressView

Double check that your application does not reuse the same ID in two different places仔细检查您的应用程序是否不会在两个不同的地方重复使用相同的 ID

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.我有一个不同的问题:在一个布局中,根是一个 ScrollView,而在另一个布局中,根是一个包含 ScrollView 的 RelativeView。 All widgets ids were identical and there were no duplicates.所有小部件 ID 都相同,并且没有重复项。 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.一旦我匹配了布局(所以在根中要么是RelativeLayout要么是ScrollView),旋转就可以正常工作了。

Not sure why this would cause a crash so any insights would be appreciated...不知道为什么这会导致崩溃,所以任何见解将不胜感激......

In my case my portrait xml has Relativelayout and ScrollView from landscape xml with the same ID.在我的情况下,我的纵向 xml 具有来自具有相同 ID 的横向 xml 的RelativelayoutScrollView

On my activity class I try to inflate the layout and assign it on ViewGroup在我的活动课上,我尝试扩大布局并将其分配给ViewGroup

So when screen rotation occur it throws: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ScrollView$SavedState所以当屏幕旋转发生时它抛出: 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这是因为ScrollView扩展了FrameLayoutRelativelayout扩展了ViewGroup

So i just wrap the landscape xml with Relativelayout and solves the problem所以我只是用Relativelayout包装景观xml并解决问题

In my case there were two different views with same id.就我而言,有两个具有相同 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.我更改了其中一个 ID。

I got this crash messsage on rotation when I had a GridView in two different layouts, one portrait and the other landscape.当我在两种不同的布局(一种纵向和另一种横向)中使用 GridView 时,我在旋转时收到此崩溃消息。 One was contained in a FrameLayout and the other was not, ie, it was by itself in the layout file.一个包含在 FrameLayout 中,另一个不包含,即,它单独存在于布局文件中。 When I removed the wrapping FrameLayout, everything worked fine.当我删除包装 FrameLayout 时,一切正常。 (Note: the GridView was used on the master side of a master/view layout.) (注意:GridView 用于主/视图布局的主端。)

The message did not appear until I upgraded my Gradle settings to this:直到我将 Gradle 设置升级到此消息后,该消息才出现:

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. Marshmallow 似乎对这个错误更无情。

I needed to use different layouts with same Id.我需要使用具有相同 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. super.onRestoreInstanceState(savedInstanceState)只能执行到点异常发生。 However, I don't find it useful, at least in my apps, so I can afford to bypass onRestoreInstanceState .但是,我觉得它没有用,至少在我的应用程序中没有用,所以我可以绕过onRestoreInstanceState

您可以在某些情况下使用: isSaveEnabled = false 用于具有相同 id 的视图,以避免在恢复状态期间出错

It happened to me with a <ProgressBar /> with android:id="@+id/progress" - without any id value clash.它发生在我身上, <ProgressBar />带有android:id="@+id/progress" - 没有任何 id 值冲突。 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.更改为android:id="@+id/progressBar"或任何解决问题的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM