gpt4 book ai didi

java - Android 游戏(Llama 或 Duck)内存不足错误

转载 作者:行者123 更新时间:2023-12-01 10:06:32 26 4
gpt4 key购买 nike

我对 Android 还很陌生,正在尝试重新创建一个旧的 iPhone 游戏(Llama 或 Duck),在其中你会看到一张图片,然后选择图片描绘的动物。该代码适用于几张图片,然后在屏幕上的任何图片的 onCreate 方法中出现 OutOfMemory 错误。我查看了其他问题,发现有一种位图方法(回收)可以解决该问题,但我不知道如何实现它或它是否与我的问题相关。感谢任何和所有的帮助。

package com.example.ryan.llamaorduck;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.util.Random;

public class DuckScreen extends AppCompatActivity {

boolean isLlamaScreen = false; //false because this is the Duck screen
Intent intent;
int screen; // variable used just for finding the next random screen

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duck_screen);
}

// user clicked on the llama button.
//method implementation decides if they are correct and either ends the
//game or moves them onto the next random page.
public void llamaClick(View view){
if (isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
startActivity(intent);
}
}
//user clicked on the button indicating that they think the animal is a
//duck. Same idea as method before, testing other button.
public void duckClick(View view){
if (!isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
startActivity(intent);
}
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.ryan.llamaorduck.DuckScreen">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:src="@drawable/duck"
android:scaleType="centerCrop"
android:id="@+id/duck"
/>

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/duck"
android:orientation="vertical"
>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Duck"
android:textSize="30sp"
android:onClick="duckClick"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Llama"
android:textSize="30sp"
android:onClick="llamaClick"
/>
</RadioGroup>

</RelativeLayout>

最佳答案

如果您不需要保留 Activity 历史记录,一种解决方案是在启动新 Activity 时使用FLAG_ACTIVITY_CLEAR_TOP 标志。所以你的代码将是这样的:

package com.example.ryan.llamaorduck;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.util.Random;

public class DuckScreen extends AppCompatActivity {

boolean isLlamaScreen = false; //false because this is the Duck screen
Intent intent;
int screen; // variable used just for finding the next random screen

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duck_screen);
}

// user clicked on the llama button.
//method implementation decides if they are correct and either ends the
//game or moves them onto the next random page.
public void llamaClick(View view){
if (isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
//user clicked on the button indicating that they think the animal is a
//duck. Same idea as method before, testing other button.
public void duckClick(View view){
if (!isLlamaScreen){
Random r = new Random();
screen = r.nextInt(2);
if (screen == 1){ //DUCK
intent = new Intent(DuckScreen.this,DuckScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else{
intent = new Intent(DuckScreen.this,LlamaScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}else{
intent = new Intent(DuckScreen.this,GameOver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}

关于java - Android 游戏(Llama 或 Duck)内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com