当前位置: 动力学知识库 > 问答 > 编程问答 >

Android Application Crashes With Orientation Changes

问题描述:

I have an Android page containg 3 images a background and edit text and button, and the manifest file the activity given as:

<activity

Android:name="SearchScreen"

>

</activity>

I have two XML one for potrait and another for landscape.

when orientation changes there is no problem for first some changes but when it changes frequently some more time the application crashes and the error seems like this

> Android.view.InflateException on orientation change

and also give some VM memory out of bounds.

and the error log is like bellow

 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


This error ocurs with sony erricson Xperia only

网友答案:

Add to your activity in manifest the following code

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

You can also use Async task to make this happen so that.

网友答案:

If you have heavy bitmaps in your layout try calling recycle for them in activity's onDestory method

@Override 
public void onDestroy(){
    ImageView imageViewWithHeavyBitmap = (ImageView)findViewById(R.id.imageview);
    imageViewWithHeavyBitmap.getDrawable().getBitmap().recycle();
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: