Simple Android App - Out of Memory error when runn

2019-07-18 18:32发布

I am fairly new to Android Studio and I try to make a simple app. Random Images should appear after a button is clicked. The next cannot be one that was already showed.

I have two classes. The MainActivity and RandomCardDraw.

When I run the app in the AVD Emulator, it does not crash. But sometimes I get the information "Skipped frames". When I run it on my Samsung S6 the app crashes everytime when I try to get the second image. In Android I can see a Out of Memory Error.

The images are between 100-200kb and I have about 18 of them.

Maybe you can see where I get my perfomance Issues from?

Here is my Code:

MainActivity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;



public class MainActivity extends AppCompatActivity {

    ImageView cardView;
    Button nextCard;
    RandomCardDraw2 game1 = new RandomCardDraw2();
    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        game1.createRandomCardList();
        gameStart();


    }

    public void gameStart()
    {
        cardView = (ImageView) findViewById(R.id.cardView);
        nextCard = (Button) findViewById(R.id.nextCard);
        nextCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                if (counter < 19)
                {
                    int id = getResources().getIdentifier(game1.drawnCards.get(counter), "drawable", getPackageName());
                    cardView.setImageResource(id);
                    counter++;
                }

            }
        });

    }

}

And RandowmCardDraw:

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


public class RandomCardDraw2
{
    ArrayList<String> drawnCards = new ArrayList<>();
    Random rndInt = new Random();

    public void createRandomCardList()
    {
        boolean gameType = true;
        int counter;
        if (gameType == true)
        {
            counter = 19;
            for (int i = 0; i < counter; i++)
            {
                String cardId = "card" + rndInt.nextInt(18 + 1);
                drawnCards.add(cardId);
            }
        }
        else
        {
            counter = 23;
            for (int i = 0; i < counter; i++)
            {
                String cardId = "card" + rndInt.nextInt(18 + 1);
                drawnCards.add(cardId);
            }
        }

    }
}

When the app is opened. The ArrayList with cards is filled randomly. And then when the user clicks the button, the next card appears. As I said, on my smartphone the app crashes when I want to get the second image.

The ErrorCode:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.kajet.shutupanddrink, PID: 18109
              java.lang.OutOfMemoryError: Failed to allocate a 192000012 byte allocation with 16775360 free bytes and 69MB until OOM
                  at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
                  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)
                  at android.content.res.Resources.loadDrawableForCookie(Resources.java:4215)
                  at android.content.res.Resources.loadDrawable(Resources.java:4089)
                  at android.content.res.Resources.getDrawable(Resources.java:2005)
                  at android.content.res.Resources.getDrawable(Resources.java:1987)
                  at android.support.v7.widget.ResourcesWrapper.getDrawable(ResourcesWrapper.java:133)
                  at android.content.Context.getDrawable(Context.java:464)
                  at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
                  at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
                  at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
                  at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
                  at com.example.kajet.shutupanddrink.MainActivity$1.onClick(MainActivity.java:41)
                  at android.view.View.performClick(View.java:5702)
                  at android.widget.TextView.performClick(TextView.java:10888)
                  at android.view.View$PerformClick.run(View.java:22541)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7229)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Can anyone help?

Thanks very much!

1条回答
姐就是有狂的资本
2楼-- · 2019-07-18 18:57

add these in your manifest these lines:

android:hardwareAccelerated="false"
android:largeHeap="true"

@your manifest.xml would like be:

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
查看更多
登录 后发表回答