首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓游戏(Llama或Duck) OutOfMemory错误

安卓游戏(Llama或Duck) OutOfMemory错误
EN

Stack Overflow用户
提问于 2016-04-05 02:22:48
回答 1查看 40关注 0票数 0

我对Android非常陌生,我正在尝试重新创建一个旧的iPhone游戏(拉玛或鸭子),在游戏中你可以看到一张图片,你可以选择这幅画所描绘的动物。代码对一些图片起作用,然后在onCreate方法中出现一个onCreate错误,无论哪个图片都在屏幕上。我看过其他问题,发现有一个位图方法(循环)可以解决这个问题,但我不知道如何实现它,也不知道它是否与我的问题相关。任何和所有的帮助都是感激的。

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-05 02:31:28

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

代码语言:javascript
复制
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);
    }
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36416042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档