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

后端 未结 4 709
情话喂你
情话喂你 2020-12-06 06:33

My app gets crashed frequently when it goes from background to foreground. Scenerio: Suppose iam playing any games and my app is in recent list and after playing,if i select

4条回答
  •  天命终不由人
    2020-12-06 07:18

    This can also happen in this edge 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 AttributeSet to that child View:

    toggleButton = new SquareToggleButton(ctx, attributeSet);
    

    As passing the AttributeSet causes the child view to have the same id as the parent and thus Android tries to restore the parents SavedState to that child view or vice versa. Instead omit the AttributeSet parameter altogether, like this:

    toggleButton = new SquareToggleButton(ctx);
    

提交回复
热议问题