简单的Android应用-在智能手机上运行时出现内存不足错误 [英] Simple Android App - Out of Memory error when running on smartphone

查看:95
本文介绍了简单的Android应用-在智能手机上运行时出现内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android Studio的新手,我尝试制作一个简单的应用程序.单击按钮后,应显示随机图像.下一个不能是已经显示的.

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.

我有两节课. MainActivity和RandomCardDraw.

I have two classes. The MainActivity and RandomCardDraw.

当我在AVD模拟器中运行该应用程序时,它不会崩溃.但是有时我会收到跳过的帧"信息.当我在Samsung S6上运行该程序时,每次尝试获取第二张图像时,该应用程序都会崩溃.在Android中,我可以看到内存不足错误.

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.

图像在100-200kb之间,我大约有18张.

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

也许您可以看到我从哪里获得我的性能问题?

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

这是我的代码:

MainActivity:

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++;
                }

            }
        });

    }

}

和RandowmCardDraw:

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);
            }
        }

    }
}

打开应用程序时.带有卡片的ArrayList是随机填充的.然后,当用户单击按钮时,将显示下一张卡片.正如我所说,当我想要获取第二张图片时,我的智能手机上的应用程序崩溃了.

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.

错误代码:

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)

任何人都可以帮忙吗?

非常感谢!

推荐答案

将这些添加到清单中的这些行:

add these in your manifest these lines:

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

@您的manifest.xml可能是:

@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">

这篇关于简单的Android应用-在智能手机上运行时出现内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆