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?
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 ([email protected])
*/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With