屏幕方向改变后应用程序崩溃

Spo*_*ook 4 crash android screen-orientation

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

我已将设备连接到计算机并以调试模式运行应用程序.重新启动后,甚至在调用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]  
Run Code Online (Sandbox Code Playgroud)

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

我只覆盖了onCreate Activity的方法.我还有自定义应用程序类,它创建了一个应用程序范围的使用引擎类的实例:

public class ProCalcApplication extends Application
{
    private Engine engine;

    public ProCalcApplication()
    {
        super();

        engine = new Engine();
    }

    public Engine getEngine()
    {
        return engine;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?


我做了一些测试.我已经注释掉了整个代码,只留下了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> 
Run Code Online (Sandbox Code Playgroud)

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

我的环境:HTC Desire Z(2.2.1),API 8使用.Eclipse版本:Helios Service Release 2构建ID: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>
Run Code Online (Sandbox Code Playgroud)

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

我错过了什么?

Spo*_*ook 10

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

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

解决方案是手动处理屏幕方向更改(请参阅Android上的活动重启)或使用FrameLayout手动实现视图更改和动画,查看可见性和动画类.

另一个是使用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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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