我对Android很陌生,我正在尝试重新创建一个旧的iPhone游戏(Llama或Duck),你可以在其中看到一张图片,然后选择图片所描绘的动物。该代码适用于一些图片,然后在onCreate方法中出现OutOfMemory错误,无论屏幕上的图片是什么。我已经查看了其他问题并看到有一个Bitmap方法(回收)可以解决问题,但我不知道如何实现它或它是否与我的问题相关。任何和所有的帮助表示赞赏。
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>
答案 0 :(得分:1)
如果您不需要保留活动历史记录,一种解决方案是在开始新活动时使用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);
}
}
}