我对Android Studio还比较新,尝试制作一个简单的应用程序。点击按钮后应该出现随机图片。下一张图片不能是已经显示过的。
我有两个类,MainActivity和RandomCardDraw。
当我在AVD模拟器中运行应用程序时,它不会崩溃。但有时会出现“跳帧”的信息。当我在我的三星S6上运行它时,每次尝试获取第二张图片时应用程序都会崩溃。在Android中,我可以看到内存不足错误。
这些图片的大小在100-200kb之间,我大约有18张图片。
也许您可以看出我遇到的性能问题来自哪里?
这是我的代码:
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++;
}
}
});
}
}
并且随机卡牌抽取:
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会随机填充。然后当用户点击按钮时,下一张卡片会出现。就像我所说的,在我的智能手机上,当我想获取第二张图像时,该应用程序会崩溃。
错误代码:
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)
有人能帮忙吗?
非常感谢!
cardView.setImageResource(id);
是罪魁祸首,因为它使用 BitmapFactory.decodeResourceStream,这会导致位图变得太大而无法在可用内存中使用。(从底部向上阅读堆栈跟踪)。 - greenapps