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

时间:2011-05-03 12:03:23

标签: android crash screen-orientation

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

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

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 建造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:两个屏幕方向更改,无论方向如何,屏幕都保持横向模式(应用程序不会崩溃)。

我错过了什么?

2 个答案:

答案 0 :(得分:10)

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

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

解决方案是手动处理屏幕方向更改(请参阅Activity restart on rotation 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布局文件中(您必须完全限定它,例如。&lt; com.myapp.SafeViewFlipper />)。

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

答案 1 :(得分:0)

这适用于你

package com.palewar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class ThreadActivity extends Activity {


    static ProgressDialog dialog;
    private Thread downloadThread;
    final static Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            dialog.dismiss();

        }

    };

    protected void onDestroy() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
        super.onDestroy();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        downloadThread = (Thread) getLastNonConfigurationInstance();
        if (downloadThread != null && downloadThread.isAlive()) {
            dialog = ProgressDialog.show(ThreadActivity.this, "",
                    "Signing in...", false);
        }

        dialog = ProgressDialog.show(ThreadActivity.this, "",
                "Signing in ...", false);

        downloadThread = new MyThread();
        downloadThread.start();
        // processThread();
    }

    // Save the thread
    @Override
    public Object onRetainNonConfigurationInstance() {
        return downloadThread;
    }


    static public class MyThread extends Thread {
        @Override
        public void run() {

            try {
                // Simulate a slow network
                try {
                    new Thread().sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0);

            } finally {

            }
        }
    }

}