我有以下问题。启动后,应用程序工作正常 - 即使在更改屏幕方向后也是如此。应用程序尚未准备好处理方向更改(例如,替代布局等),因此只会出现旋转的默认布局,这没问题。但是,当我按返回键离开应用程序时,更改方向并在再次启动应用程序后立即崩溃。崩溃后,如果我再次启动应用程序,它运行良好,直到前面描述的情况发生 - 然后它崩溃。

我已将设备连接到计算机并在 Debug模式下运行应用程序。重新启动后,甚至在调用 onCreate 之前抛出异常。崩溃调用堆栈如下:

Thread [<1> main] (Suspended (exception IllegalArgumentException))
WindowManagerImpl.removeViewImmediate(View) line: 262
Window$LocalWindowManager.removeViewImmediate(View) line: 436
ActivityThread.handleDestroyActivity(IBinder, boolean, int, boolean) line: 4022
ActivityThread.handleRelaunchActivity(ActivityThread$ActivityRecord, int) line: 4127
ActivityThread.access$2400(ActivityThread, ActivityThread$ActivityRecord, int) line: 136
ActivityThread$H.handleMessage(Message) line: 2183
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 143
ActivityThread.main(String[]) line: 5068
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 858
ZygoteInit.main(String[]) line: 616
NativeStart.main(String[]) line: not available [native method]

我计划稍后处理屏幕旋转,但在那之前,我希望默认行为能够正常工作。

我只覆盖了 onCreate Activity 的方法。我还有自定义应用程序类,它创建了一个应用程序范围内使用的引擎类的实例:
public class ProCalcApplication extends Application
{
    private Engine engine;

    public ProCalcApplication()
    {
        super();

        engine = new Engine();
    }

    public Engine getEngine()
    {
        return engine;
    }
}

如何解决这个问题呢?

我做了更多的测试。我已经注释掉了整个代码,只留下 onCreate 方法的默认实现(super() + setContentLayout())。问题仍然存在,所以我已经注释掉了整个布局 XML,应用程序终于停止了崩溃。我正在确定错误的条目,请稍等;)

我找到了原因,但没有解决方案。错误的 XML 代码如下:

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <android.gesture.GestureOverlayView android:id="@+id/gestureOverlay" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3">
        <ViewFlipper android:id="@+id/contextArea" android:layout_width="match_parent" android:layout_height="match_parent">

        </ViewFlipper>
    </android.gesture.GestureOverlayView>

</LinearLayout>

有人可以尝试证明或反驳此代码在描述的情况下失败吗?或者指出,我哪里出错了;)

我的环境:HTC Desire Z (2.2.1),使用 API 8。 Eclipse 版本:Helios Service Release 2
内部版本号:20110218-0911。

编辑:让它短一点:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
        <ViewFlipper android:id="@+id/contextArea" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3">
        </ViewFlipper>
</LinearLayout>

还有更多信息;模拟器中的 API 8:两个屏幕方向更改 (Ctrl+F12) 和应用程序崩溃。模拟器中的 API 10:两个屏幕方向发生变化,无论方向如何,屏幕都保持横向模式(尽管应用程序不会崩溃)。

我错过了什么?

最佳答案

我发现了,我错过了什么 :) 由于没有人回答,我会为每个人留下一个答案,他们会遇到同样的问题。

事实证明,所描述的问题是一个众所周知的 Android 库错误:ViewFlipper 无法正确处理屏幕方向更改。它出现在 API 2.1 中并一直持续到 3.0,在那里它被认为是固定的。不幸的是,今天的大多数智能手机都存在这个问题,因为它们通常有 2.2 或 2.3 板载。

解决方案是手动处理屏幕方向更改(请参阅 Activity restart on rotation Android )或手动实现 View 更改和动画,使用 FrameLayout、 View 可见性和动画类。

另一种是使用 Eric Burke 的 SafeViewFlipper 类:

/**
 * Works around Android Bug 6191 by catching IllegalArgumentException after
 * detached from the window.
 *
 * @author Eric Burke (eric@squareup.com)
 */
public class SafeViewFlipper extends ViewFlipper {
  public SafeViewFlipper(Context context) {
    super(context);
  }

  public SafeViewFlipper(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  /**
   * Workaround for Android Bug 6191:
   * http://code.google.com/p/android/issues/detail?id=6191
   * <p/>
   * ViewFlipper occasionally throws an IllegalArgumentException after
   * screen rotations.
   */
  @Override protected void onDetachedFromWindow() {
    try {
      super.onDetachedFromWindow();
    } catch (IllegalArgumentException e) {
      Log.d(TAG, "SafeViewFlipper ignoring IllegalArgumentException");

      // Call stopFlipping() in order to kick off updateRunning()
      stopFlipping();
    }
  }
}

您可以在从代码创建布局时使用它,也可以将其嵌入到您的 xml 布局文件中(您必须完全限定它,例如 )。

有关更多信息,另请参阅 http://code.google.com/p/android/issues/detail?id=6191

关于android - 屏幕方向更改后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5869153/

10-11 22:28
查看更多