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

4
我有一个问题。在启动后,应用程序运行良好 - 即使更改屏幕方向也是如此。该应用程序尚未准备处理方向更改(例如,备用布局等),因此只会显示旋转的默认布局,这很好。但是,当我按下返回键离开应用程序,更改方向并立即重新启动应用程序时,它会崩溃。崩溃后,如果我再次启动应用程序,则在先前描述的情况发生时它会崩溃。
我已将设备连接到计算机并以调试模式运行应用程序。重启后,在调用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 Build 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>

另外还有一些信息:在模拟器中,API 8进行两次屏幕方向更改(Ctrl+F12)会导致应用程序崩溃。在模拟器中,API 10进行两次屏幕方向更改后,屏幕始终处于横向模式,而不管其方向如何(尽管应用程序不会崩溃)。

我错过了什么?


你正在使用哪个设备进行测试?以及使用的API级别是多少?你是否注意到你的引擎类在屏幕方向改变后重新初始化了吗?它应该这样做还是不应该这样做? - Nathan Fig
HTC Desire Z。API级别8。方向更改重启后,引擎不会重新初始化;它不应该这样做,但即使如此也没有什么损害(尽管会略微降低性能)。 - Spook
你检查过重启时引擎是否为空了吗?另外,在清单文件中,你是否将ProCalcApplication明确注册为Application的扩展? - Nathan Fig
我没有。但事实是,应用程序处于非常早期的阶段,虽然引擎已经创建,但尚未被使用。因此,它很不可能是问题的原因(我无论如何都会检查一下)。ProCalcApplication被设置为Application的扩展,因为我使用了Eclipse向导,该向导在设置清单字段时创建了ProCalcApplication(我也会检查一下,以确保;))。 - Spook
我已经禁用了自定义应用程序类(实际上禁用了引擎的创建),但问题仍然存在。看起来是GestureOverlayView与ViewFlipper结合使用导致的。不过为什么会这样还不清楚。 - Spook
1个回答

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

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

另请参阅http://code.google.com/p/android/issues/detail?id=6191获取更多信息。


我发现它停止并抛出了异常。而且这个错误已经出现了。Android 7.1.2,小米红米4x,miui全球版8.5。 - Sergio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接