应用程序在加载图片时运行“内存不足”

时间:2014-09-15 15:47:32

标签: java android logcat

我编写了一个小应用程序,向您显示一个图像,如果它是“A”或类型“B”,您必须在不到一秒的时间内点击两个按钮中的一个。 它运行良好,但当加载或多或少的第5张图片时,应用程序内存不足。

这是堆栈跟踪:

java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
    at android.content.res.Resources.loadDrawable(Resources.java:2110)
    at android.content.res.Resources.getDrawable(Resources.java:700)
    at android.view.View.setBackgroundResource(View.java:15303)
    at com.pitazzo.bootyfocus.GameActivity.load(GameActivity.java:50)
    at com.pitazzo.bootyfocus.GameActivity.onCreate(GameActivity.java:40)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)

这是崩溃的应用程序的整个代码:

package com.pitazzo.bootyfocus;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GameActivity extends Activity implements OnClickListener {

    public static int score = 0;
    TextView tv;
    String choosen = "";
    List<Integer> list = new ArrayList<Integer>();
    Random r = new Random();
    RelativeLayout layout;
    int id;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        tv = (TextView) findViewById(R.id.foto);
        list.add(R.drawable.culo1);
        list.add(R.drawable.culo2);
        list.add(R.drawable.culo3);
        list.add(R.drawable.tetas1);
        list.add(R.drawable.tetas2);
        list.add(R.drawable.tetas3);
        layout = (RelativeLayout) findViewById(R.id.activity_game);
        load();

        findViewById(R.id.tetaBTN).setOnClickListener(this);
        findViewById(R.id.culoBTN).setOnClickListener(this);

    }

    public void load() {
        id = list.get(r.nextInt(list.size()));

        tv.setText("Recurso: "+getResources().getResourceEntryName(id).toString());
        layout.setBackgroundResource(id);
        handler.postDelayed(runnable, 1000);

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == findViewById(R.id.tetaBTN).getId()) {
            choosen = "teta";
        } else {
            choosen = "culo";
        }

        if (getResources().getResourceEntryName(id).toString().contains("teta")) {
            if (choosen == "teta") {
                success();
            } else {
                fail();
            }
        } else if (getResources().getResourceEntryName(id).toString()
                .contains("culo")) {
            if (choosen == "culo") {
                success();
            } else {
                fail();
            }
        }

    }

    public void success() {
        score++;
        handler.removeCallbacks(runnable);
        load();

    }

    public void fail() {
        Intent i = new Intent(this, FailActivity.class);
        startActivity(i);
        handler.removeCallbacks(runnable);

    }

    private Runnable runnable = new Runnable() {
           @Override
           public void run() {
             fail();
           }
        };

    @Override
    public void onBackPressed() {

    }

}

在此先感谢,任何帮助将不胜感激

0 个答案:

没有答案