Java.lang.ClassCastException: android.view.AbsSavedState $ 1 cannot be added to android.support.v7.widget.Toolbar $ SavedState

My application often crashes when it goes from the background to the foreground. Sceneio: Suppose that I play any games, and my application is on the last list, and after the game, if I select the application, it will fail and show an error. There is no toolbar in my application. Only I used the action bar.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fcords.android/com.fcords.android.Home.HomeScreen.HomePage_New}: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.support.v7.widget.Toolbar$SavedState at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) at android.app.ActivityThread.access$800(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.support.v7.widget.Toolbar$SavedState at android.support.v7.widget.Toolbar.onRestoreInstanceState(Toolbar.java:1048) at android.view.View.dispatchRestoreInstanceState(View.java:13639) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2889) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2895) at android.view.View.restoreHierarchyState(View.java:13617) at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1982) at android.app.Activity.onRestoreInstanceState(Activity.java:1032) at android.app.Activity.performRestoreInstanceState(Activity.java:987) at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1184) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2287) 

Anyone facing this problem? Thanks in advance.

+8
source share
3 answers

The problem is fixed in my case:

The problem in my case is:

1: I have an id in xml that has the same name as the layout.

that is: in my case, I have a custom action bar layout with the name "action_bar.xml" and an identifier in another layout as "+ id / action_bar". So this problem occurs when the application is not in memory and while recreating this page.

NOTE: SHOULD NOT USE IDENTIFIER IDENTIFICATION / IDENTIFICATION more than once.

+18
source

This can also happen in this extreme case:

If you create a custom view programmatically inside the constructor of another parent view that has the AttributeSet parameter:

  public ToggleButtonDescriptive(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.toggleButton = new SquareToggleButton(ctx, attributeSet); } 

DO NOT pass the attribute set to this child view:

 toggleButton = new SquareToggleButton(ctx, attributeSet); 

Since passing the AttributeSet attribute causes the child view to have the same identifier as the parent view, and thus, Android is trying to restore the parent SavedState to that child view or vice versa. Instead, do not specify the AttributeSet parameter at all, for example:

 toggleButton = new SquareToggleButton(ctx); 
+2
source

In my case, I have a screen layout with ChipGroup and chips without identifiers. Then, if I return to this screen from others, I received this error "java.lang.ClassCastException: android.view.AbsSavedState $ 1 cannot be cast to android.widget.CompoundButton $ SavedState". So I just added id to the chip and everything works fine.

0
source

Source: https://habr.com/ru/post/1215041/


All Articles