4

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.

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.

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?


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


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> 

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

My environment: HTC Desire Z (2.2.1), API 8 used. Eclipse version: Helios Service Release 2 Build id: 20110218-0911.

Edit: Make it a little shorter:

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

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

What did I miss?

Spook
  • 25,318
  • 18
  • 90
  • 167
  • What device are you testing with? And what API level? Have you noticed whether your engine class is re-initializing on post-orientation-change startup? Should it, or should it not? – Nathan Fig May 03 '11 at 12:28
  • HTC Desire Z. API level 8. The engine is not reinitialized after the orientation change restart; it is not supposed to do so, however there's no harm if it did (despite a small performance decrease). – Spook May 03 '11 at 12:40
  • Have you checked whether the engine is null when you restart? Also, have you registered ProCalcApplication specifically as an extension of Application in the manifest? – Nathan Fig May 03 '11 at 13:53
  • I didn't. But the truth is, that the application is in so early stage, that though engine is created, it is not yet used. So it's very unlikely, that it is the cause of problem (I'll check that anyway). The ProCalcApplication is set as extension of Application, because I've used the Eclipse wizard, which created the ProCalcApplication while setting up fields of the manifest (And I'll check it too, just to be sure ;)) – Spook May 03 '11 at 17:53
  • I've disabled the custom application class (which, in effect, disabled creation of the engine) and the problem persisted. It seems, that GestureOverlayView combined with ViewFlipper causes it. No idea why, though. – Spook May 04 '11 at 08:42

1 Answers1

10

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.

Community
  • 1
  • 1
Spook
  • 25,318
  • 18
  • 90
  • 167
  • I found that it stops throw this exception. And the bug has arived. Android 7.1.2, Xiaomi redmi 4x, miui global 8.5 – Sergio Nov 17 '17 at 11:13