Application crashes after screen orientation changed

后端 未结 2 414
傲寒
傲寒 2021-02-06 15:46

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 c

2条回答
  •  余生分开走
    2021-02-06 16:27

    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
       * 

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

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

提交回复
热议问题