8

所以我正在开发的这个应用程序在一部非常旧的可怜的 Android 2.3.3 手机上运行良好。但是,当在 GS4 上运行它时,每次必须从可绘制对象加载资源时,GS4 都会抛出内存异常,这是在 xml 中预定义的。

<ImageView
        android:id="@+id/ivLearnMore"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:scaleType="fitCenter"
        android:src="@drawable/learn_more" />

例如,这个 Imageview 在扩展布局时会抛出异常,因为它已经分配了一个图像。(256x256)

到底是怎么回事?

05-08 10:33:54.972: E/dalvikvm-heap(24423): Out of memory on a 16777232-byte allocation.
05-08 10:33:54.972: I/dalvikvm(24423): "main" prio=5 tid=1 RUNNABLE
05-08 10:33:54.972: I/dalvikvm(24423):   | group="main" sCount=0 dsCount=0 obj=0x41187b38 self=0x40dd1b68
05-08 10:33:54.972: I/dalvikvm(24423):   | sysTid=24423 nice=0 sched=0/0 cgrp=apps handle=1074533852
05-08 10:33:54.972: I/dalvikvm(24423):   | state=R schedstat=( 0 0 0 ) utm=147 stm=1265 core=0
05-08 10:33:54.972: I/dalvikvm(24423):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.content.res.Resources.loadDrawable(Resources.java:2854)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.view.View.<init>(View.java:3460)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.view.ViewGroup.<init>(ViewGroup.java:446)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.widget.LinearLayout.<init>(LinearLayout.java:176)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.widget.LinearLayout.<init>(LinearLayout.java:172)
05-08 10:33:54.972: I/dalvikvm(24423):   at java.lang.reflect.Constructor.constructNative(Native Method)
05-08 10:33:54.972: I/dalvikvm(24423):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.view.LayoutInflater.createView(LayoutInflater.java:593)
05-08 10:33:54.972: I/dalvikvm(24423):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-08 10:33:54.972: I/dalvikvm(24423):   at com.travelstorysgps.travelstorys.FragmentInfo.onCreateView(FragmentInfo.java:54)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.os.Handler.handleCallback(Handler.java:725)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.os.Looper.loop(Looper.java:137)
05-08 10:33:54.972: I/dalvikvm(24423):   at android.app.ActivityThread.main(ActivityThread.java:5293)
05-08 10:33:54.972: I/dalvikvm(24423):   at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:33:54.972: I/dalvikvm(24423):   at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:33:54.972: I/dalvikvm(24423):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-08 10:33:54.972: I/dalvikvm(24423):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-08 10:33:54.972: I/dalvikvm(24423):   at dalvik.system.NativeStart.main(Native Method)
05-08 10:33:54.972: D/skia(24423): --- decoder->decode returned false
05-08 10:33:54.972: D/AndroidRuntime(24423): Shutting down VM
05-08 10:33:54.972: W/dalvikvm(24423): threadid=1: thread exiting with uncaught exception (group=0x41186ac8)
05-08 10:33:54.992: E/AndroidRuntime(24423): FATAL EXCEPTION: main
05-08 10:33:54.992: E/AndroidRuntime(24423): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at com.travelstorysgps.travelstorys.FragmentInfo.onCreateView(FragmentInfo.java:54)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.os.Handler.handleCallback(Handler.java:725)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.os.Looper.loop(Looper.java:137)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.app.ActivityThread.main(ActivityThread.java:5293)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at dalvik.system.NativeStart.main(Native Method)
05-08 10:33:54.992: E/AndroidRuntime(24423): Caused by: java.lang.reflect.InvocationTargetException
05-08 10:33:54.992: E/AndroidRuntime(24423):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.LayoutInflater.createView(LayoutInflater.java:593)
05-08 10:33:54.992: E/AndroidRuntime(24423):    ... 21 more
05-08 10:33:54.992: E/AndroidRuntime(24423): Caused by: java.lang.OutOfMemoryError
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.content.res.Resources.loadDrawable(Resources.java:2854)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.View.<init>(View.java:3460)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.view.ViewGroup.<init>(ViewGroup.java:446)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.widget.LinearLayout.<init>(LinearLayout.java:176)
05-08 10:33:54.992: E/AndroidRuntime(24423):    at android.widget.LinearLayout.<init>(LinearLayout.java:172)
05-08 10:33:54.992: E/AndroidRuntime(24423):    ... 24 more

这是来自不同屏幕的异常

05-08 10:35:29.565: E/dalvikvm-heap(24774): Out of memory on a 16777232-byte allocation.
05-08 10:35:29.565: I/dalvikvm(24774): "main" prio=5 tid=1 RUNNABLE
05-08 10:35:29.565: I/dalvikvm(24774):   | group="main" sCount=0 dsCount=0 obj=0x41187b38 self=0x40dd1b68
05-08 10:35:29.565: I/dalvikvm(24774):   | sysTid=24774 nice=0 sched=0/0 cgrp=apps handle=1074533852
05-08 10:35:29.565: I/dalvikvm(24774):   | state=R schedstat=( 0 0 0 ) utm=175 stm=2086 core=1
05-08 10:35:29.565: I/dalvikvm(24774):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.content.res.Resources.loadDrawable(Resources.java:2854)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.view.View.<init>(View.java:3460)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.view.ViewGroup.<init>(ViewGroup.java:446)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.widget.LinearLayout.<init>(LinearLayout.java:176)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.widget.LinearLayout.<init>(LinearLayout.java:172)
05-08 10:35:29.565: I/dalvikvm(24774):   at java.lang.reflect.Constructor.constructNative(Native Method)
05-08 10:35:29.565: I/dalvikvm(24774):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.view.LayoutInflater.createView(LayoutInflater.java:593)
05-08 10:35:29.565: I/dalvikvm(24774):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-08 10:35:29.565: I/dalvikvm(24774):   at com.travelstorysgps.travelstorys.FragmentDonate.onCreateView(FragmentDonate.java:53)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.os.Handler.handleCallback(Handler.java:725)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.os.Looper.loop(Looper.java:137)
05-08 10:35:29.565: I/dalvikvm(24774):   at android.app.ActivityThread.main(ActivityThread.java:5293)
05-08 10:35:29.565: I/dalvikvm(24774):   at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:35:29.565: I/dalvikvm(24774):   at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:35:29.565: I/dalvikvm(24774):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-08 10:35:29.565: I/dalvikvm(24774):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-08 10:35:29.565: I/dalvikvm(24774):   at dalvik.system.NativeStart.main(Native Method)
05-08 10:35:29.565: D/AbsListView(24774): [unregisterDoubleTapMotionListener]
05-08 10:35:29.565: I/MotionRecognitionManager(24774):   .unregisterListener : / listener count = 0->0,  
05-08 10:35:29.565: D/skia(24774): --- decoder->decode returned false
05-08 10:35:29.565: D/AbsListView(24774): unregisterIRListener() is called 
05-08 10:35:29.565: D/AndroidRuntime(24774): Shutting down VM
05-08 10:35:29.565: W/dalvikvm(24774): threadid=1: thread exiting with uncaught exception (group=0x41186ac8)
05-08 10:35:29.595: E/AndroidRuntime(24774): FATAL EXCEPTION: main
05-08 10:35:29.595: E/AndroidRuntime(24774): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at com.travelstorysgps.travelstorys.FragmentDonate.onCreateView(FragmentDonate.java:53)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.os.Handler.handleCallback(Handler.java:725)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.os.Looper.loop(Looper.java:137)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.app.ActivityThread.main(ActivityThread.java:5293)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at dalvik.system.NativeStart.main(Native Method)
05-08 10:35:29.595: E/AndroidRuntime(24774): Caused by: java.lang.reflect.InvocationTargetException
05-08 10:35:29.595: E/AndroidRuntime(24774):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.LayoutInflater.createView(LayoutInflater.java:593)
05-08 10:35:29.595: E/AndroidRuntime(24774):    ... 21 more
05-08 10:35:29.595: E/AndroidRuntime(24774): Caused by: java.lang.OutOfMemoryError
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.content.res.Resources.loadDrawable(Resources.java:2854)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.View.<init>(View.java:3460)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.view.ViewGroup.<init>(ViewGroup.java:446)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.widget.LinearLayout.<init>(LinearLayout.java:176)
05-08 10:35:29.595: E/AndroidRuntime(24774):    at android.widget.LinearLayout.<init>(LinearLayout.java:172)
05-08 10:35:29.595: E/AndroidRuntime(24774):    ... 24 more

这是第三个不同的屏幕

05-08 10:37:20.113: E/dalvikvm-heap(25740): Out of memory on a 16777232-byte allocation.
05-08 10:37:20.123: I/dalvikvm(25740): "main" prio=5 tid=1 RUNNABLE
05-08 10:37:20.123: I/dalvikvm(25740):   | group="main" sCount=0 dsCount=0 obj=0x41187b38 self=0x40dd1b68
05-08 10:37:20.123: I/dalvikvm(25740):   | sysTid=25740 nice=0 sched=0/0 cgrp=apps handle=1074533852
05-08 10:37:20.123: I/dalvikvm(25740):   | state=R schedstat=( 0 0 0 ) utm=156 stm=1070 core=1
05-08 10:37:20.123: I/dalvikvm(25740):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.content.res.Resources.loadDrawable(Resources.java:2854)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.view.View.<init>(View.java:3460)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.view.ViewGroup.<init>(ViewGroup.java:446)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.widget.LinearLayout.<init>(LinearLayout.java:176)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.widget.LinearLayout.<init>(LinearLayout.java:172)
05-08 10:37:20.123: I/dalvikvm(25740):   at java.lang.reflect.Constructor.constructNative(Native Method)
05-08 10:37:20.123: I/dalvikvm(25740):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.view.LayoutInflater.createView(LayoutInflater.java:593)
05-08 10:37:20.123: I/dalvikvm(25740):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-08 10:37:20.123: I/dalvikvm(25740):   at com.travelstorysgps.travelstorys.FragmentConnect.onCreateView(FragmentConnect.java:73)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.os.Handler.handleCallback(Handler.java:725)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.os.Looper.loop(Looper.java:137)
05-08 10:37:20.123: I/dalvikvm(25740):   at android.app.ActivityThread.main(ActivityThread.java:5293)
05-08 10:37:20.123: I/dalvikvm(25740):   at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:37:20.123: I/dalvikvm(25740):   at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:37:20.123: I/dalvikvm(25740):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-08 10:37:20.123: I/dalvikvm(25740):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-08 10:37:20.123: I/dalvikvm(25740):   at dalvik.system.NativeStart.main(Native Method)
05-08 10:37:20.123: D/skia(25740): --- decoder->decode returned false
05-08 10:37:20.123: D/AndroidRuntime(25740): Shutting down VM
05-08 10:37:20.123: W/dalvikvm(25740): threadid=1: thread exiting with uncaught exception (group=0x41186ac8)
05-08 10:37:20.133: E/AndroidRuntime(25740): FATAL EXCEPTION: main
05-08 10:37:20.133: E/AndroidRuntime(25740): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at com.travelstorysgps.travelstorys.FragmentConnect.onCreateView(FragmentConnect.java:73)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.os.Handler.handleCallback(Handler.java:725)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.os.Looper.loop(Looper.java:137)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.app.ActivityThread.main(ActivityThread.java:5293)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at java.lang.reflect.Method.invoke(Method.java:511)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at dalvik.system.NativeStart.main(Native Method)
05-08 10:37:20.133: E/AndroidRuntime(25740): Caused by: java.lang.reflect.InvocationTargetException
05-08 10:37:20.133: E/AndroidRuntime(25740):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.LayoutInflater.createView(LayoutInflater.java:593)
05-08 10:37:20.133: E/AndroidRuntime(25740):    ... 21 more
05-08 10:37:20.133: E/AndroidRuntime(25740): Caused by: java.lang.OutOfMemoryError
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.content.res.Resources.loadDrawable(Resources.java:2854)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.View.<init>(View.java:3460)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.view.ViewGroup.<init>(ViewGroup.java:446)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.widget.LinearLayout.<init>(LinearLayout.java:176)
05-08 10:37:20.133: E/AndroidRuntime(25740):    at android.widget.LinearLayout.<init>(LinearLayout.java:172)
05-08 10:37:20.133: E/AndroidRuntime(25740):    ... 24 more
4

3 回答 3

16

(@Arash 的回答提供了一种解决方法,但这是我试图解释它为什么会起作用的尝试。)

因此,您在资源目录中有一个相当大的图像drawable,您在布局 xml 文件中引用它,并在运行时对其进行膨胀时出现内存不足错误。

在加载可绘制资源时,如果 Android 认为有必要,它会执行一些预缩放。官方文档

根据当前屏幕的密度,系统使用应用程序中任何特定于大小或密度的资源,并在不缩放的情况下显示它们。如果资源在正确的密度下不可用,系统会加载默认资源并根据需要放大或缩小它们以匹配当前屏幕的密度。系统假定默认资源(来自没有配置限定符的目录的那些)是为基线屏幕密度 (mdpi) 设计的,除非它们是从特定于密度的资源目录加载的。

这意味着两件事:

  1. 如果没有应用程序当前运行的环境密度的资源,系统将获取另一个密度的资源并对其进行缩放(向上或向下)以匹配目标密度
  2. drawable假定放置在目录中的资源以mdpi密度为目标

因此,您当前正在 xxhdpi 手机上运行。系统想要加载learn_more可绘制资源以将其绘制到ivLearnMore小部件上。它将drawable-xxhdpi在优先级中查找它,但不会在那里找到它,因此将采用它找到的最接近的那个,在这种情况下是drawable目录中的那个(它假设它以 mdpi 密度为目标),并将其加载到内存中如果您的图像文件非常大并且很容易需要比应用程序可用的内存更多的内存(并导致内存不足错误),则将其缩放3 倍。

这也是为什么它可以在你的旧 2.3 设备上正常加载的原因:该设备最有可能是 mdpi 或 hdpi 密度,系统没有尝试升级它或仅将其放大 1.5 倍。

这里的主要内容是您将适合 xxhdpi(或更多?)密度的资源放在 mdpi 目录中。

所以你有几个选择:

  1. 为每个密度提供一个适当大小的资源(即每个drawable-*目录中一个):如果可能,这是避免任何问题的最佳方法
  2. 如果您只有一个版本的资源,请将其放在与其密度匹配的目录中(drawable相当于drawable-mdpi!),在您的情况下可能drawable-xxhdpi(取决于您的资源)
  3. 将资源放在drawable-nodpi目录中,这将阻止系统对资源执行任何预缩放(但这通常适用于与密度无关的资源,例如您在运行时自行调整大小)
于 2014-10-21T21:09:52.380 回答
7

我有同样的问题,那就是:Galaxy S4所有 1920*1080 分辨率的设备,xxhdpi首先检查文件夹,如果没有特定的图像,它会缩放其他尺寸,例如xhdpi,......所以我将我的图片移动到xxhdpi文件夹并解决了!

于 2014-02-18T08:07:53.007 回答
6

我发现了这个问题。背景图像显然没有得到非常有效的处理,尤其是对于 1920x1080 的屏幕。因此,您应该使用自定义 xml 可绘制对象、纯色或 24 位 jpg。有趣的是,我刚刚进行了一些测试,1920x1080 jpeg 占用了 95.60 KB,而渲染为 png 的相同图像占用了 929.09 KB。

所以我做了一些测试,发现xml定义的带有渐变等的drawables非常快,然后我尝试了我的1920x1080 jpg,它非常快,所以我输入了一个1920x1080 png并得到了错误。这对我来说很有趣,因为我在开始时为高清屏幕设置的 png 仅为 910x540,我会收到错误消息。

所以这里的教训是,如果你想要一个图像背景,请使用 jpg。

于 2013-05-08T17:23:47.763 回答