android VM不会让我们在Button的setBackgroundResource上分配10458756字节错误

时间:2014-11-11 20:26:54

标签: java android eclipse button imagebutton

我只想在点击时更改我按钮的图像。只需按下"clicked""not clicked"的按钮 我的按钮图像尺寸不大:164 KB

我的代码与android version 2.3.3一起正常运行 在最新版本上我开始得到错误

我在互联网上阅读了很多东西但仍无法找到答案。

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == (MotionEvent.ACTION_UP)){
                //Do whatever you want after press                  
                center_button.setBackgroundResource(R.drawable.button);
            }
            if(event.getAction() == (MotionEvent.ACTION_DOWN)){             
                //Do whatever you want during press
                //((BitmapDrawable)center_button.getDrawable()).getBitmap().recycle();
                center_button.setBackgroundResource(R.drawable.clicked);
                count++;
            }
            return false;

我已经尝试了以下所有事情但没有帮助:

    更改图片前
  1. ((BitmapDrawable)center_button.getDrawable()).getBitmap().recycle();
  2. 更改为Imagebutton
  3. 添加带回收的销毁方法
  4. 错误:

    11-11 20:09:18.305: D/dalvikvm(932): GC_EXTERNAL_ALLOC freed 19K, 52% free 2605K/5379K, external 12929K/14977K, paused 43ms
    11-11 20:09:18.625: D/dalvikvm(932): GC_EXTERNAL_ALLOC freed 10K, 52% free 2594K/5379K, external 17468K/19516K, paused 29ms
    11-11 20:09:18.645: E/dalvikvm-heap(932): 10458756-byte external allocation too large for this process.
    11-11 20:09:18.675: E/GraphicsJNI(932): VM won't let us allocate 10458756 bytes
    11-11 20:09:18.675: D/dalvikvm(932): GC_FOR_MALLOC freed 0K, 52% free 2594K/5379K, external 17468K/19516K, paused 17ms
    11-11 20:09:18.675: D/AndroidRuntime(932): Shutting down VM
    11-11 20:09:18.675: W/dalvikvm(932): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    11-11 20:09:18.695: E/AndroidRuntime(932): FATAL EXCEPTION: main
    11-11 20:09:18.695: E/AndroidRuntime(932): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.Bitmap.nativeCreate(Native Method)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.content.res.Resources.getDrawable(Resources.java:581)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.view.View.setBackgroundResource(View.java:7533)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at com.lyuhanan.button.MainActivity$1.onTouch(MainActivity.java:93)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.view.View.dispatchTouchEvent(View.java:3881)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.os.Handler.dispatchMessage(Handler.java:99)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.os.Looper.loop(Looper.java:123)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at java.lang.reflect.Method.invoke(Method.java:507)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    11-11 20:09:18.695: E/AndroidRuntime(932):  at dalvik.system.NativeStart.main(Native Method)
    11-11 20:09:30.735: I/Process(932): Sending signal. PID: 932 SIG: 9
    

1 个答案:

答案 0 :(得分:0)

我刚刚将Android Emulator中的VM编号从32更改为320,现在正在运行。 我还是不明白为什么以前失败了。