屏幕方向后,应用程序崩溃改变 [英] Application crashes after screen orientation changed

查看:191
本文介绍了屏幕方向后,应用程序崩溃改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。启动后,应用程序工作正常 - 甚至改变屏幕的方向后该应用程序还没有ppared处理方向变化(如替代布局等)$ P $,所以只需旋转的默认布局出现,它的确定。但是,当我离开由pressing返回键的应用程序,更改方向,并立即后重新启动应用程序,它崩溃。崩溃后,如果我再一次启动应用程序,它工作得很好,直到pviously描述的$ P $的情况下发生的 - 那么它崩溃

I've got the following problem. After starting, application works fine - even after changing the screen orientation. The application is not yet prepared to handle orientation change (eg. alternative layout and so on), so just the rotated default layout appears and it's OK. However, when I leave the application by pressing the back key, change the orientation and immediately after start the application again, it crashes. After a crash, if I start application once again, it works well until the previously described circumstances occur - then it crashes.

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

I've connected the device to computer and run the application in debug mode. After restarting, an exception is thrown even before calling onCreate. The crash callstack follows:

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]  

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

I plan to handle screen rotation later, but until then, I wish the default behavior to work correctly.

我重写只有活动的onCreate的方法。我也得到了自定义的应用程序类,它创建应用程序范围内使用的引擎类的一个实例:

I've overriden only the onCreate Activity's method. I've also got custom application class, which creates an instance of application-wide used engine class:

public class ProCalcApplication extends Application
{
    private Engine engine;

    public ProCalcApplication()
    {
        super();

        engine = new Engine();
    }

    public Engine getEngine()
    {
        return engine;
    }
}

如何解决这个问题呢?

How to solve this problem?

我已经做了一些更多的测试。我注释掉整个code,剩下的onCreate方法只有默认的实现(超()+ setContentLayout())。问题依然存在,所以我注释掉整个布局XML和应用程序终于停止了崩溃。我在钉下的故障录入过程中,请站在;)

I've done some more testing. I've commented out whole code, leaving only the default implementation of onCreate method (super() + setContentLayout()). The problem persisted, so I've commented out whole layout XML and the application finally stopped crashing. I'm in process of nailing down the faulty entry, please stand by ;)

我已经找到了原因,还没有解决方案。该故障的XML code如下:

I've found the cause, yet no solution. The faulty XML code follows:

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

可有人可能试图证明或证伪,这code中所描述的情况下失败?或者指出,在哪里我犯了一个错误;)

Can someone maybe try to prove or disprove, that this code fails in described circumstances? Or point out, where did I make a mistake ;)

我的环境:HTC Desire Z的(2.2.1),API 8中使用。 Eclipse版本:太阳神服务版本2 构建编号:20110218-0911

My environment: HTC Desire Z (2.2.1), API 8 used. Eclipse version: 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仿真器:两个屏幕方向变化和屏幕保持在横向模式下,不管方向(应用程序不会崩溃,虽然)

And a little more info; API 8 in emulator: two screen orientation changes (Ctrl+F12) and application crashes. API 10 in emulator: two screen orientation changes and the screen remains in landscape mode regardless of orientation (application does not crash, though).

我错过了什么?

推荐答案

我发现了,我怎么错过了:)既然没有人回答,我会留下给大家一个答案,谁就会遇到同样的问题

I've found out, what did I missed :) Since no one answered, I'll leave an answer for everyone, who'll encounter the same problem.

事实证明,所描述的问题是通常已知的Andr​​oid库的错误:ViewFlipper未能妥善处理屏幕方向变化。它已经出现在API 2.1,并继续直到3.0,在那里它被认为是固定的。但是很不幸,今天的大多数智能手机遭遇这个问题,因为他们通常有2.2或2.3船上。

It turns out, that the problem described is a generally known Android libraries bug: ViewFlipper fails to handle screen orientation change properly. It have appeared in API 2.1 and continues until 3.0, where it is believed to be fixed. Unfortunatelly, most of today's smartphones suffer from this problem, as usually they have 2.2 or 2.3 onboard.

解决方法是手动处理屏幕方向变化(见旋转的Andr​​oid 活动启动)或手动实现视图的变化和动画,使用的FrameLayout,查看可视性和动画类。

The solution is either to handle screen orientation change manually (see Activity restart on rotation Android ) or implement the view changes and animations manually, using FrameLayout, view visibility and animation classes.

另一种是用埃里克·伯克的SafeViewFlipper类:

Another one is to use Eric Burke's SafeViewFlipper class:

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

您可以使用它同时创造了code中的布局,以及将它嵌入到XML布局文件(你必须完全限定它,EG&LT;。com.myapp.SafeViewFlipper /&GT;)

You can use it while creating the layout from the code as well as embed it into your xml layout file (you'll have to qualify it fully, eg. <com.myapp.SafeViewFlipper />).

另请参见<一个href="http://$c$c.google.com/p/android/issues/detail?id=6191">http://$c$c.google.com/p/android/issues/detail?id=6191更多的信息。

这篇关于屏幕方向后,应用程序崩溃改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆