所以我正在编写一个软件,当我尝试创建第二个drawable时它会一直崩溃。它从前面的屏幕开始,然后我按下一个按钮,然后进入一个新的活动。在那个活动中,我在中间有一个ImageView,在底部有一个按钮。当活动加载时,ImageView中有一个drawable。因此,当我点击底部的按钮时,它应该更改ImageView中的drawable,但它会崩溃。我在LogCat中发现了一些错误,例如:
E/AndroidRuntime(1385): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
所以这是我的活动代码:
package com.foocity.android.gui;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class NewMapActivity extends Activity
{
public ImageView mapPreview;
public Drawable currentPic;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newmap);
currentPic = getResources().getDrawable(R.drawable.milestone_cleanenergy);
mapPreview = (ImageView) findViewById(R.id.chooseCanvas);
mapPreview.setImageDrawable(currentPic);
}
public void changePic(View view)
{
System.out.println("tester boy!!!!!!!!!!!");
currentPic = null;
currentPic = getResources().getDrawable(R.drawable.welcomescreen);
mapPreview.setImageDrawable(currentPic);
}
}
这就是LogCat在崩溃时所说的内容:
06-13 21:25:56.613: D/dalvikvm(4086): GC_EXTERNAL_ALLOC freed 614 objects / 50072 bytes in 58ms
06-13 21:26:02.133: W/System.err(4086): java.io.FileNotFoundException: /sdcard/000.txt (No such file or directory)
06-13 21:26:02.143: W/System.err(4086): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
06-13 21:26:02.143: W/System.err(4086): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
06-13 21:26:02.143: W/System.err(4086): at java.io.FileInputStream.<init>(FileInputStream.java:82)
06-13 21:26:02.143: W/System.err(4086): at java.util.Scanner.<init>(Scanner.java:188)
06-13 21:26:02.143: W/System.err(4086): at java.util.Scanner.<init>(Scanner.java:167)
06-13 21:26:02.143: W/System.err(4086): at com.foocity.android.Map.<init>(Map.java:33)
06-13 21:26:02.143: W/System.err(4086): at com.foocity.android.FooCity.setMap(FooCity.java:594)
06-13 21:26:02.143: W/System.err(4086): at com.foocity.android.gui.StartActivity.chooseMap(StartActivity.java:33)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:02.153: W/System.err(4086): at android.view.View$1.onClick(View.java:2067)
06-13 21:26:02.153: W/System.err(4086): at android.view.View.performClick(View.java:2408)
06-13 21:26:02.153: W/System.err(4086): at android.view.View$PerformClick.run(View.java:8816)
06-13 21:26:02.153: W/System.err(4086): at android.os.Handler.handleCallback(Handler.java:587)
06-13 21:26:02.153: W/System.err(4086): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 21:26:02.153: W/System.err(4086): at android.os.Looper.loop(Looper.java:123)
06-13 21:26:02.153: W/System.err(4086): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:02.153: W/System.err(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:02.153: W/System.err(4086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-13 21:26:02.153: W/System.err(4086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-13 21:26:02.153: W/System.err(4086): at dalvik.system.NativeStart.main(Native Method)
06-13 21:26:02.393: D/dalvikvm(4086): GC_EXTERNAL_ALLOC freed 3101 objects / 159528 bytes in 53ms
06-13 21:26:07.113: I/System.out(4086): tester boy!!!!!!!!!!!
06-13 21:26:07.123: E/dalvikvm-heap(4086): 9469350-byte external allocation too large for this process.
06-13 21:26:07.123: E/GraphicsJNI(4086): VM won't let us allocate 9469350 bytes
06-13 21:26:07.133: D/AndroidRuntime(4086): Shutting down VM
06-13 21:26:07.133: W/dalvikvm(4086): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-13 21:26:07.143: E/AndroidRuntime(4086): FATAL EXCEPTION: main
06-13 21:26:07.143: E/AndroidRuntime(4086): java.lang.IllegalStateException: Could not execute method of the activity
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View$1.onClick(View.java:2072)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View.performClick(View.java:2408)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View$PerformClick.run(View.java:8816)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.os.Handler.handleCallback(Handler.java:587)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.os.Looper.loop(Looper.java:123)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:07.143: E/AndroidRuntime(4086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-13 21:26:07.143: E/AndroidRuntime(4086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-13 21:26:07.143: E/AndroidRuntime(4086): at dalvik.system.NativeStart.main(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): Caused by: java.lang.reflect.InvocationTargetException
06-13 21:26:07.143: E/AndroidRuntime(4086): at com.foocity.android.gui.NewMapActivity.changePic(NewMapActivity.java:30)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.view.View$1.onClick(View.java:2067)
06-13 21:26:07.143: E/AndroidRuntime(4086): ... 11 more
06-13 21:26:07.143: E/AndroidRuntime(4086): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.nativeCreate(Native Method)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.content.res.Resources.loadDrawable(Resources.java:1709)
06-13 21:26:07.143: E/AndroidRuntime(4086): at android.content.res.Resources.getDrawable(Resources.java:581)
06-13 21:26:07.143: E/AndroidRuntime(4086): ... 15 more
这显然与内存管理有关,我不习惯使用Java进行监控,但我不确定该怎么做。任何帮助,将不胜感激。感谢
P.S。我意识到发布这个,我尝试了其他东西(创建一个文件对象,然后将其内容写入System.out),它甚至在新活动加载ImageView中的第一个drawable之前就引起了同样的错误,所以它必须要做的一切有了内存管理,我想我需要帮助来管理这个系统中的内存,因为我已经习惯了常规Java中的GC处理。
答案 0 :(得分:1)
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
从这一行可以看出,您尝试显示的图像尺寸过大。尝试减小图像的大小,看看。
答案 1 :(得分:0)
首先想到的是,您尝试将一个图像绘制在另一个图像上,结果您的应用程序内存不足。我不确定如何,但在绘制下一张图像之前尝试清除ImageView。
编辑:Displaying Bitmaps efficiently 这应该可以帮助您解决问题。
答案 2 :(得分:0)
06-13 21:26:07.123:E / dalvikvm-heap(4086):9469350字节的外部分配对于此过程来说太大了。 06-13 21:26:07.123:E / GraphicsJNI(4086):VM不允许我们分配9469350字节
这表明您用作R.drawable.welcomescreen
的图像资源太大(其位图需要大约9 MB)。我坚信你可以缩小图像资源(在将它放到“drawable”文件夹之前),因此它不会占用太多内存。
答案 3 :(得分:0)
我在xml中使用android:src =“@ drawable / blah.jpg”设置的第一个活动上的ImageView,而不是像我第二次做的那样。当我从xml中删除该行并使用我在第二个活动ImageView中执行的方法在第一个活动上设置pic时,一切正常。现在的问题是为什么这有效?为什么在xml中设置pic占用了更多的内存空间?