Application crashes after screen orientation changed

对着背影说爱祢 提交于 2019-12-03 04:51:28
Spook

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.

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.

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.

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

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 />).

See also http://code.google.com/p/android/issues/detail?id=6191 for more informations.

This Works for you

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 {

            }
        }
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!