我有一个错误“java.lang.OutOfMemoryError”怎么修复它? (对不起这种糟糕的日志格式,我不写很多代码文本)
我的activity_main.xml,错误此
<ImageView
android:layout_width="350dp"
android:layout_height="700dp"
android:id="@+id/imageViewCosmonaut"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-50dp"
android:src="@drawable/background_cosmonaut"/>
logcat的
05-05 16:04:10.313 2475-2475/com.vlad.genius.game E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vlad.genius.game/com.vlad.genius.game.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class android.widget.ImageView
...
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.widget.ImageView
...
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
at android.content.res.Resources.loadDrawable(Resources.java:2105)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1895)
at com.vlad.genius.game.MainActivity.onCreate(MainActivity.java:23)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
显然,您分配给图片视图的位图太大,无法加载到您的应用空间中。你必须找到另一种方式。
通常对于大位图,在XML中分配它们不是一个好主意。在UI线程中发生XML的通货膨胀并加载大位图,即使它不会导致OOM,也会偶尔导致ANR。
我强烈建议您阅读this article,它为您提供了非常有价值的课程,如何以正确的方式处理Android中的图像。