Android:“缩放到填充屏幕”和“拉伸到填充屏幕”

时间:2012-01-31 13:17:02

标签: android layout view zoom stretch

当我在zoom to fill screenstretch to fill screen之间切换时,我的应用程序将崩溃。顺便说一句,我注意到市场上很多应用程序都存在这个问题。有谁知道如何避免这个问题。

这是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

2 个答案:

答案 0 :(得分:5)

似乎我找到了另一种方式,阅读你的帖子:给小部件布局不同的id,根据这样的方向进行实例化:

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;
}

答案 1 :(得分:2)

问题出在xml布局中,我使用layout-normal中的scrollView和layout-xlarge中的listView作为相同的viewId。当Android尝试恢复视图状态时,这将导致应用程序崩溃。

一个不错的解决方法是覆盖方法onSaveInstaceStateonRestoreInstaceState,这样就可以避免classCastException