简体   繁体   English

Android游戏(骆驼或鸭)OutOfMemory错误

[英]Android game (Llama or Duck) OutOfMemory Error

I am pretty new to Android and am trying to recreate an old iPhone game (Llama or Duck) where you see a picture, and you choose which animal the picture depicts. 我刚接触Android,正在尝试重新创建旧的iPhone游戏(骆驼或鸭),您可以在其中看到图片,并选择图片所描绘的动物。 The code works for a few pictures, then comes an OutOfMemory error in the onCreate method for whichever picture was on the screen. 该代码适用于几张图片,然后针对屏幕上显示的任何图片,在onCreate方法中都会出现OutOfMemory错误。 I have looked at other questions and seen that there is a Bitmap method (recycle) that might solve the problem but I do not know how to implement it or whether or not it is relevant to my problem. 我看过其他问题,发现有一种位图方法(循环)可以解决问题,但我不知道如何实现它或它是否与我的问题有关。 Any and all help is appreciated. 任何和所有帮助表示赞赏。

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:

<?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>

If you don't need to keep the history of activities, one solution is to use FLAG_ACTIVITY_CLEAR_TOP flag when starting a new activity. 如果不需要保留活动的历史记录,一种解决方案是在开始新活动时使用FLAG_ACTIVITY_CLEAR_TOP标志。 So your code would be like: 因此,您的代码如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM