private class Roller implements Runnable
{
private long delayMillis;
public Roller(long delayMillis)
{
this.delayMillis = delayMillis;
}
@Override
public void run()
{
if (tv != null)
{
int min = 0;
int max = 3;
int n = rand.nextInt(max - min + 1) + min;
String roll = String.valueOf(n);
tv.setText("Random number is " + roll);
if (roll.equals("0")) {
ibRed.setImageResource(R.drawable.red_selector);
ibGreen.setImageResource(R.drawable.green_dark);
ibBlue.setImageResource(R.drawable.blue_dark);
ibYellow.setImageResource(R.drawable.yellow_dark);
}
if (roll.equals("1")) {
ibRed.setImageResource(R.drawable.red_dark);
ibGreen.setImageResource(R.drawable.green_selector);
ibBlue.setImageResource(R.drawable.blue_dark);
ibYellow.setImageResource(R.drawable.yellow_dark);
}
if (roll.equals("2")) {
ibRed.setImageResource(R.drawable.red_dark);
ibGreen.setImageResource(R.drawable.green_dark);
ibBlue.setImageResource(R.drawable.blue_selector);
ibYellow.setImageResource(R.drawable.yellow_dark);
}
if (roll.equals("3")) {
ibRed.setImageResource(R.drawable.red_dark);
ibGreen.setImageResource(R.drawable.green_dark);
ibBlue.setImageResource(R.drawable.blue_dark);
ibYellow.setImageResource(R.drawable.yellow_selector);
}
tv.postDelayed(this, delayMillis);
}
}
}
在Activity中运行上面的代码后,一段时间后我收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp, PID: 4601
java.lang.OutOfMemoryError: Failed to allocate a 12960012 byte allocation with 1400312 free bytes and 1367KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2453)
at android.content.res.Resources.loadDrawable(Resources.java:2360)
at android.content.res.Resources.getDrawable(Resources.java:768)
at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:133)
at android.content.Context.getDrawable(Context.java:402)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:344)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:185)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:69)
at android.support.v7.widget.AppCompatImageButton.setImageResource(AppCompatImageButton.java:69)
at com.myapp.MainActivity$Roller.run(MainActivity.java:85)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
避免这种情况的最佳方法是什么?抽签每个约26KB。