具体报错信息如下:
java.lang.RuntimeException:Unable to start activity ComponentInfo{com.fsgx.car.android.igo/com.fsgx.car.android.igo.usercenter.mytrips.MyTripsActivity}: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.HorizontalScrollView$SavedState
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2878)
......
Caused by:
java.lang.ClassCastException:android.view.AbsSavedState$1 cannot be cast to android.widget.HorizontalScrollView$SavedState
android.widget.HorizontalScrollView.onRestoreInstanceState(HorizontalScrollView.java:1702)
android.view.View.dispatchRestoreInstanceState(View.java:15781)
android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3271)
android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3277)
android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3277)
android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3277)
android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3277)
android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3277)
android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3277)
android.view.View.restoreHierarchyState(View.java:15759)
com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2130)
android.app.Activity.onRestoreInstanceState(Activity.java:1154)
android.app.Activity.performRestoreInstanceState(Activity.java:1109)
android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1175)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2841)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2940)
android.app.ActivityThread.-wrap12(ActivityThread.java)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1663)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:159)
android.app.ActivityThread.main(ActivityThread.java:6385)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1096)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:883)
报错原因:
创建控件的时候在其构造方法里动态创建了一个子控件,并把该控件的
AttributeSet
参数传递给了子控件。
public SlidingTabLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
... ...
mTabStrip = new SlidingTabStrip(context,attrs);
... ...
}
传递AttributeSet给子控件会导致子控件与父控件拥有相同的
id
,因此Android会尝试将父控件的SavedState恢复给子控件,反之亦然。因此,会导致类型转换bug。
版权声明:本文为chenrenxiang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。