在创建带有少量图像的布局时,我有OutOfMemoryError。
在logcat显示错误的行中,我开始声明TextView。
三星Galaxy S4出现错误。 当我在HTC Desire X上运行应用程序时没有错误。
可能是什么原因以及如何解决这个问题。
10-21 18:32:19.243: E/dalvikvm-heap(20015): Out of memory on a 15822016-byte allocation.
10-21 18:32:19.243: I/dalvikvm(20015): "main" prio=5 tid=1 RUNNABLE
10-21 18:32:19.243: I/dalvikvm(20015): | group="main" sCount=0 dsCount=0 obj=0x41d6cea0 self=0x41d5b538
10-21 18:32:19.243: I/dalvikvm(20015): | sysTid=20015 nice=0 sched=0/0 cgrp=apps handle=1074303316
10-21 18:32:19.243: I/dalvikvm(20015): | state=R schedstat=( 0 0 0 ) utm=235 stm=203 core=2
10-21 18:32:19.243: I/dalvikvm(20015): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-21 18:32:19.243: I/dalvikvm(20015): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
10-21 18:32:19.243: I/dalvikvm(20015): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
10-21 18:32:19.243: I/dalvikvm(20015): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
10-21 18:32:19.243: I/dalvikvm(20015): at android.content.res.Resources.loadDrawable(Resources.java:3056)
10-21 18:32:19.243: I/dalvikvm(20015): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
10-21 18:32:19.253: I/dalvikvm(20015): at android.view.View.<init>(View.java:3694)
10-21 18:32:19.263: I/dalvikvm(20015): at android.widget.TextView.<init>(TextView.java:898)
10-21 18:32:19.263: I/dalvikvm(20015): at android.widget.TextView.<init>(TextView.java:893)
10-21 18:32:19.263: I/dalvikvm(20015): at java.lang.reflect.Constructor.constructNative(Native Method)
10-21 18:32:19.263: I/dalvikvm(20015): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
10-21 18:32:19.263: I/dalvikvm(20015): at android.view.LayoutInflater.createView(LayoutInflater.java:600)
10-21 18:32:19.263: I/dalvikvm(20015): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-21 18:32:19.263: I/dalvikvm(20015): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
10-21 18:32:19.263: I/dalvikvm(20015): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
10-21 18:32:19.263: I/dalvikvm(20015): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
10-21 18:32:19.263: I/dalvikvm(20015): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
10-21 18:32:19.263: I/dalvikvm(20015): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
10-21 18:32:19.263: I/dalvikvm(20015): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
10-21 18:32:19.263: I/dalvikvm(20015): at com.example.fragments.LocalApiFragment.onCreateView(LocalApiFragment.java:40)
10-21 18:32:19.263: I/dalvikvm(20015): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
10-21 18:32:19.263: I/dalvikvm(20015): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
10-21 18:32:19.263: I/dalvikvm(20015): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-21 18:32:19.263: I/dalvikvm(20015): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-21 18:32:19.263: I/dalvikvm(20015): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-21 18:32:19.263: I/dalvikvm(20015): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
10-21 18:32:19.263: I/dalvikvm(20015): at android.os.Handler.handleCallback(Handler.java:733)
10-21 18:32:19.263: I/dalvikvm(20015): at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 18:32:19.263: I/dalvikvm(20015): at android.os.Looper.loop(Looper.java:157)
10-21 18:32:19.263: I/dalvikvm(20015): at android.app.ActivityThread.main(ActivityThread.java:5356)
10-21 18:32:19.263: I/dalvikvm(20015): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 18:32:19.263: I/dalvikvm(20015): at java.lang.reflect.Method.invoke(Method.java:515)
10-21 18:32:19.263: I/dalvikvm(20015): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-21 18:32:19.263: I/dalvikvm(20015): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-21 18:32:19.263: I/dalvikvm(20015): at dalvik.system.NativeStart.main(Native Method)
10-21 18:32:19.263: D/skia(20015): --- allocation failed for scaled bitmap
10-21 18:32:19.263: D/AndroidRuntime(20015): Shutting down VM
10-21 18:32:19.263: W/dalvikvm(20015): threadid=1: thread exiting with uncaught exception (group=0x41d6bda0)
10-21 18:32:19.263: E/AndroidRuntime(20015): FATAL EXCEPTION: main
10-21 18:32:19.263: E/AndroidRuntime(20015): Process: com.example, PID: 20015
10-21 18:32:19.263: E/AndroidRuntime(20015): android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.createView(LayoutInflater.java:626)
10-21 18:32:19.263: E/AndroidRuntime(20015): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
10-21 18:32:19.263: E/AndroidRuntime(20015): at com.example.fragments.LocalApiFragment.onCreateView(LocalApiFragment.java:40)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.os.Handler.handleCallback(Handler.java:733)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.os.Looper.loop(Looper.java:157)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.app.ActivityThread.main(ActivityThread.java:5356)
10-21 18:32:19.263: E/AndroidRuntime(20015): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 18:32:19.263: E/AndroidRuntime(20015): at java.lang.reflect.Method.invoke(Method.java:515)
10-21 18:32:19.263: E/AndroidRuntime(20015): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
10-21 18:32:19.263: E/AndroidRuntime(20015): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
10-21 18:32:19.263: E/AndroidRuntime(20015): at dalvik.system.NativeStart.main(Native Method)
10-21 18:32:19.263: E/AndroidRuntime(20015): Caused by: java.lang.reflect.InvocationTargetException
10-21 18:32:19.263: E/AndroidRuntime(20015): at java.lang.reflect.Constructor.constructNative(Native Method)
10-21 18:32:19.263: E/AndroidRuntime(20015): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.LayoutInflater.createView(LayoutInflater.java:600)
10-21 18:32:19.263: E/AndroidRuntime(20015): ... 23 more
10-21 18:32:19.263: E/AndroidRuntime(20015): Caused by: java.lang.OutOfMemoryError
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.content.res.Resources.loadDrawable(Resources.java:3056)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.view.View.<init>(View.java:3694)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.widget.TextView.<init>(TextView.java:898)
10-21 18:32:19.263: E/AndroidRuntime(20015): at android.widget.TextView.<init>(TextView.java:893)
10-21 18:32:19.263: E/AndroidRuntime(20015): ... 26 more
答案 0 :(得分:0)
你的位图太重了。调整大小(例如gimp)并重试。
答案 1 :(得分:0)
我在大多数HiDef设备(例如Galaxy S4)中偶然发现了这个问题。这是因为HiDef设备使用xhdpi,xxhdpi等来扩展drawable。当他们没有找到它时,他们以编程方式对其进行扩展,从而导致OutOfMemory Exception。因此,解决方案是将您的drawable放在那些hdpi,xhdpi,xxhdpi,xxhdpi文件夹上。