在没有明显原因的情况下,应用程序在方向更改后崩溃

时间:2011-06-30 03:23:01

标签: android landscape screen-orientation portrait

EDIT-Moderator,随时删除此问题。问题在于我的背景图片大小,而不是实际的方向更改。


请忍受我 - 我不相信这个问题/问题已经用这个简单的术语来回答。

我有一个应用程序,其中包含多个活动和2个布局资源,每个活动具有相同的XML文件名称,存储在res / layout-land和res / layout-port中。

问题在于,当我在Nexus One上启动应用程序(我使用的是Android 2.3)时,应用程序启动时没有任何问题,但如果我尝试旋转屏幕(更改方向),它就会崩溃。为了澄清,如果我启动持有手机横向样式的Activity,应用程序会显示res / layout-land资源布局,反之亦然,但如果我尝试在运行时更改方向,则会崩溃。

我相信如果有2个XML布局,保存在-port和-land文件夹中,那么Android应该销毁活动并使用相同名称的其他布局重新创建它,对吗?

这是我的清单,如果有必要,我很乐意粘贴更多代码。

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

这是onCreate方法的开头:

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

其余部分是动作侦听器,如果单击每个按钮,它会启动另一个活动的意图。主菜单布局对应于res / layout-land / mainmenu&amp; res / layout-port / mainmenu,至少在理论上。

我查看了DDMS错误,但没有任何链接指向代码中存在错误的地方。

编辑:错误登录请求(所有红色东西,直到强制关闭) -

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

从本质上讲,我的问题是:为了使自动屏幕方向更改起作用,需要采取哪些步骤?我以为我做了所有这些...

非常感谢任何帮助或建议,谢谢!


@Rahul Sharma- 这停止了​​崩溃,但它不是切换到其他布局,而是在不同的方向使用相同的布局。

@Asynkronos 我在2.3而不是2.1,但这肯定是一个值得考虑的有趣bug。我不认为我正在使用这些对象。

好的 - 你们会生气,但问题是我的背景图像非常接近30英寸x 30英寸。所以我把它缩小到大约7x7英寸,没有问题......

4 个答案:

答案 0 :(得分:4)

在AndroidManifest.xml中放置

所有活动标签中的

android:configChanges =“orientation | keyboardHidden”

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>

希望这会有所帮助: - )

答案 1 :(得分:0)

也在我的应用上发生,检查是否未在Orientation更改上创建活动。它确实与活动生命周期有关

答案 2 :(得分:0)

我的Nexus One也有类似的屏幕方向更改问题。也许您遇到了这个记录的错误?

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

答案 3 :(得分:0)

在您的清单中插入android:configChanges =&#34; keyboardHidden | orientation | screenSize&#34;在应用程序标签中,以克服由于方向更改而导致的崩溃这有助于我的申请。