0

我开发了一个在 android 2.1 版本上运行良好的应用程序,但是当我在 android 4.0 版本上启动它时,它不会运行并抛出这样的堆栈跟踪:


09-26 19:45:10.961: E/AndroidRuntime(785): FATAL EXCEPTION: main
09-26 19:45:10.961: E/AndroidRuntime(785): java.lang.OutOfMemoryError
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.Bitmap.nativeCreate(Native Method)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.content.res.Resources.loadDrawable(Resources.java:1937)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.content.res.Resources.getDrawable(Resources.java:664)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.widget.ImageView.resolveUri(ImageView.java:542)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.widget.ImageView.setImageResource(ImageView.java:315)
09-26 19:45:10.961: E/AndroidRuntime(785):  at com.deedatech.AnimalsNameForKids.AnimalsNameForKidsActivity.onCreate(AnimalsNameForKidsActivity.java:85)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.app.Activity.performCreate(Activity.java:4465)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.os.Looper.loop(Looper.java:137)
09-26 19:45:10.961: E/AndroidRuntime(785):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-26 19:45:10.961: E/AndroidRuntime(785):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 19:45:10.961: E/AndroidRuntime(785):  at java.lang.reflect.Method.invoke(Method.java:511)
09-26 19:45:10.961: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 19:45:10.961: E/AndroidRuntime(785):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 19:45:10.961: E/AndroidRuntime(785):  at dalvik.system.NativeStart.main(Native   Method)

我只是不明白为什么会发生这种情况,因为相同的程序在 android 2.1 版本上运行完美,但outOfMemoryError在 4.0 版本上运行。如果你有什么想法请帮忙..

4

2 回答 2

0

与 Anis 所说的相反,在不同的线程上解码您的位图不会帮助您避免内存不足 (OOM) 错误,您仍然将完全相同数量的数据加载到您的应用程序中。

您需要减小正在加载的图像的大小或对其进行下采样,这样您就不会加载所有数据,有关这方面的更多信息,请查看有效地加载大型位图

于 2012-09-26T08:03:30.220 回答
0

如果您正在从 UI 线程解码位图,则存在发生 outOfMemoryError 的风险。尝试将您的解码流更改为 asynTask。

于 2012-09-26T07:49:44.807 回答