AJcodez
AJcodez

Reputation: 34156

Application crashes after an orientation change for no apparent reason

EDIT-Moderator, feel free to delete this question. The problem lay in my background image size, and not the actual orientation change.


Please bear with me- I do not believe this question/problem has been answered in this simple of terms.

I have an application with several activities and 2 layout resources for each with the same name for the XML files, stored in res/layout-land and res/layout-port.

The issue is that when I start the application on a Nexus One (I am using Android 2.3) the application starts with no issues, but if I try to rotate the screen (change the orientation), it crashes. To clarify, if I start the Activity holding the phone landscape style, the application shows me the res/layout-land resource layout, and vice versa, but if I try to change the orientation at runtime, it crashes.

I believe that if there are 2 XML layouts, saved in -port and -land folders then Android should destroy the activity and recreate it with the other layout by the same name, correct?

Here is my manifest, and I am happy to paste in some more code if necessary.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="dominion.game"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <application android:icon="@drawable/icon" android:label="@string/app_name" debuggable="true">
        <activity android:name="dominion.game.DominionMenu"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="dominion.game.CreateMenu"
                  android:label="@string/app_name">
        </activity>
        <activity android:name="dominion.game.AdvancedMenu"
                  android:label="@string/app_name">
        </activity>
        <activity android:name="dominion.game.ShuffleResult"
                  android:label="@string/app_name">
        </activity>
    </application>
</manifest>

Here is the beginning of the onCreate method:

      @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
    //...Lots of onClick listeners for the 4 buttons...
    }

The rest of it is action listeners, and if each button is clicked, it starts an intent to another activity. The mainmenu layout corresponds to res/layout-land/mainmenu & res/layout-port/mainmenu, at least in theory.

I looked at the DDMS errors, but nothing has a link to somewhere in my code where there is an error.

EDIT: error log on request (all of the red stuff until the force close)-

07-01 00:23:41.082: ERROR/dalvikvm-heap(11182): 9864000-byte external allocation too large for this process.
07-01 00:23:41.109: ERROR/GraphicsJNI(11182): VM won't let us allocate 9864000 bytes
07-01 00:23:41.109: DEBUG/dalvikvm(11182): GC_FOR_MALLOC freed <1K, 52% free 2598K/5379K, external 19819K/19819K, paused 15ms
07-01 00:23:41.117: DEBUG/AndroidRuntime(11182): Shutting down VM
07-01 00:23:41.117: WARN/dalvikvm(11182): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182): FATAL EXCEPTION: main
07-01 00:23:41.128: ERROR/AndroidRuntime(11182): java.lang.RuntimeException: Unable to start activity ComponentInfo{dominion.game/dominion.game.DominionMenu}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2796)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.ActivityThread.access$1600(ActivityThread.java:117)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:932)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.os.Looper.loop(Looper.java:123)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at java.lang.reflect.Method.invoke(Method.java:507)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at dalvik.system.NativeStart.main(Native Method)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.Activity.setContentView(Activity.java:1657)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at dominion.game.DominionMenu.onCreate(DominionMenu.java:28)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     ... 12 more
07-01 00:23:41.128: ERROR/AndroidRuntime(11182): Caused by: java.lang.reflect.InvocationTargetException
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     ... 22 more
07-01 00:23:41.128: ERROR/AndroidRuntime(11182): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.Bitmap.nativeCreate(Native Method)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:463)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:326)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.View.<init>(View.java:1951)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.View.<init>(View.java:1899)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.view.ViewGroup.<init>(ViewGroup.java:286)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     at android.widget.LinearLayout.<init>(LinearLayout.java:120)
07-01 00:23:41.128: ERROR/AndroidRuntime(11182):     ... 25 more
07-01 00:23:41.144: WARN/ActivityManager(113):   Force finishing activity dominion.game/.DominionMenu

In essence, my question is: What are the steps needed to take in order for automatic screen orientation changes to work? I thought I did them all...

Any help or suggestions are much appreciated, thanks in advance!


@Rahul Sharma- That stopped the crashing, but instead of switching to the other layout, it just uses the same layout in the different orientation.

@Asynkronos I'm on 2.3 not 2.1 but that is an interesting bug to consider, certainly. I don't think I'm using those objects though.

OK- you guys are gonna be annoyed, but the problem was that my background image was enormous- like close to 30in x 30in. So I scaled it down to about 7x7 in inches, and there is no issue...

Upvotes: 0

Views: 3725

Answers (4)

user2713047
user2713047

Reputation: 79

In your manifest insert android:configChanges="keyboardHidden|orientation|screenSize" in application tag to overcome crash occurrence due to orientation change. This has helped my application.

Upvotes: 0

Rahul Sharma
Rahul Sharma

Reputation: 3677

in your AndroidManifest.xml put

android:configChanges="orientation|keyboardHidden" in all of your activity tags

e.g.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dominion.game"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <application android:icon="@drawable/icon" android:label="@string/app_name" debuggable="true">
        <activity android:name="dominion.game.DominionMenu" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="dominion.game.CreateMenu"
              android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
        </activity>
        <activity android:name="dominion.game.AdvancedMenu"
              android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
        </activity>
        <activity android:name="dominion.game.ShuffleResult"
              android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
        </activity>
    </application>
</manifest>

Hope this helps :-)

Upvotes: 4

Asynkronos
Asynkronos

Reputation: 195

I've had similar screen orientation change problems on my Nexus One as well. Perhaps you are encountering this documented bug?

http://code.google.com/p/android/issues/detail?id=6191

Upvotes: 0

karlstackoverflow
karlstackoverflow

Reputation: 3418

Was happening on my app aswell, check that activities arent being created on Orientation change or something. Its to do with the activity lifecycle for sure

Upvotes: 0

Related Questions