Android应用程序因方向更改而崩溃

时间:2011-11-18 06:54:08

标签: android

我有一个Android页面,其中包含3个图像背景和编辑文本和按钮,以及清单文件,其活动如下:

<activity
     Android:name="SearchScreen"
     >
</activity>

我有两个用于potrait的XML,另一个用于横向。 当方向发生变化时,对于第一次更改没有问题,但是当它经常更改时应用程序崩溃并且错误看起来像这样

> Android.view.InflateException on orientation change

并且还会使某些VM内存超出范围。

,错误日志如下左图

 11-21 15:09:55.832: E/AndroidRuntime(32448): FATAL EXCEPTION: main
  11-21 15:09:55.832: E/AndroidRuntime(32448): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.af.screenmanager/com.af.screenmanager.SearchScreen}: android.view.InflateException: Binary XML file line #61: Error inflating class <unknown>
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2853)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.ActivityThread.access$1600(ActivityThread.java:121)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.os.Handler.dispatchMessage(Handler.java:99)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.os.Looper.loop(Looper.java:130)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.ActivityThread.main(ActivityThread.java:3701)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at java.lang.reflect.Method.invokeNative(Native Method)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at java.lang.reflect.Method.invoke(Method.java:507)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at dalvik.system.NativeStart.main(Native Method)
    11-21 15:09:55.832: E/AndroidRuntime(32448): Caused by: android.view.InflateException: Binary XML file line #61: Error inflating class <unknown>
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.support.v4.app.FragmentActivity.setContentView(FragmentActivity.java:304)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at com.af.screenmanager.SearchScreen.onCreate(SearchScreen.java:131)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    ... 12 more
    11-21 15:09:55.832: E/AndroidRuntime(32448): Caused by: java.lang.reflect.InvocationTargetException
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at java.lang.reflect.Constructor.constructNative(Native Method)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    ... 24 more
    11-21 15:09:55.832: E/AndroidRuntime(32448): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:494)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:370)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.content.res.Resources.loadDrawable(Resources.java:1720)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.content.res.Resources.getDrawable(Resources.java:585)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:805)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:746)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.content.res.Resources.loadDrawable(Resources.java:1705)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.view.View.<init>(View.java:1975)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.widget.TextView.<init>(TextView.java:370)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.widget.Button.<init>(Button.java:108)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    at android.widget.Button.<init>(Button.java:104)
    11-21 15:09:55.832: E/AndroidRuntime(32448):    ... 27 more

这个错误只发生在sony erricson Xperia上

2 个答案:

答案 0 :(得分:1)

在清单中添加您的活动以下代码

<activity android:name="SearchScreen"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" android:windowSoftInputMode="stateAlwaysHidden">
</activity>

您也可以使用异步任务来实现这一目标。

答案 1 :(得分:0)

如果您的布局中有大量位图,请尝试在activity的onDestory方法中为它们调用recycle?

@Override 
public void onDestroy(){
    ImageView imageViewWithHeavyBitmap = (ImageView)findViewById(R.id.imageview);
    imageViewWithHeavyBitmap.getDrawable().getBitmap().recycle();
}