Reputation: 1043
When I switch between zoom to fill screen
and stretch to fill screen
my application will crash. By the way I noticed that a lot of app on the market have this problem. Does anyone know how to avoid this problem.
Here's the logcat:
E/AndroidRuntime(24475): FATAL EXCEPTION: main
E/AndroidRuntime(24475): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.macchetv.api/com.macchetv.activity.HomeActivity}: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.AbsListView$SavedState
E/AndroidRuntime(24475): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1818)
E/AndroidRuntime(24475): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
E/AndroidRuntime(24475): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3191)
E/AndroidRuntime(24475): at android.app.ActivityThread.access$600(ActivityThread.java:122)
E/AndroidRuntime(24475): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
E/AndroidRuntime(24475): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(24475): at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(24475): at android.app.ActivityThread.main(ActivityThread.java:4126)
E/AndroidRuntime(24475): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24475): at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(24475): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
E/AndroidRuntime(24475): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
E/AndroidRuntime(24475): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24475): Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.AbsListView$SavedState
E/AndroidRuntime(24475): at android.widget.AbsListView.onRestoreInstanceState(AbsListView.java:1650)
E/AndroidRuntime(24475): at android.view.View.dispatchRestoreInstanceState(View.java:8341)
E/AndroidRuntime(24475): at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2038)
E/AndroidRuntime(24475): at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:766)
E/AndroidRuntime(24475): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024)
E/AndroidRuntime(24475): at android.view.View.restoreHierarchyState(View.java:8320)
E/AndroidRuntime(24475): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1642)
E/AndroidRuntime(24475): at android.app.Activity.onRestoreInstanceState(Activity.java:898)
E/AndroidRuntime(24475): at android.app.Activity.performRestoreInstanceState(Activity.java:870)
E/AndroidRuntime(24475): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1101)
E/AndroidRuntime(24475): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1796)
E/AndroidRuntime(24475): ... 12 more
Upvotes: 1
Views: 1599
Reputation: 51
It seems that i've found another way, reading your post : give widgets different ids on layouts, instanciating according to orientation like this :
int ot = getResources().getConfiguration().orientation;
switch (ot)
{
case Configuration.ORIENTATION_LANDSCAPE:
lvListe = (HorizontalListView)mainView.findViewById(R.id.hlvListe);
break;
case Configuration.ORIENTATION_PORTRAIT:
lvListe = (ListView)mainView.findViewById(R.id.lvListe);
break;
default:
break;
}
Upvotes: 5
Reputation: 1043
The problem is inside the xml layouts, I use a scrollView in layout-normal
and a listView in layout-xlarge
for the same viewId. This will cause the application to crash when Android tries to restore the state of the view.
A nice workaround can be to override the methods onSaveInstaceState
and onRestoreInstaceState
, this way it's possible to avoid a classCastException
Upvotes: 2