setContentView

时间:2016-04-05 15:57:28

标签: java android performance

我对这款Llama或Duck游戏进行了更新(对现有游戏进行了重新设计),由于outOfMemory错误,应用程序非常不一致地关闭。我已多次尝试清理内存,例如使用Intent的addFlags并研究不同的解决方案,但错误仍在继续。顺便说一句,这是两个屏幕中的一个(基本相同),在用户正在玩游戏时将在整个游戏中不断重建。目标是让用户选择在屏幕上显示哪种动物,如果正确,它们将继续播放。感谢任何帮助,谢谢。

package com.example.ryan.llamaorduck;

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

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class DuckScreen extends AppCompatActivity {

boolean isLlamaScreen = false; //false because this is the Duck screen
int duck_score;
Intent intent;
int screen; // variable used just for finding the next random screen
int pic; //not currently implemented
SharedPreferences pref; //used for storing score
SharedPreferences.Editor editor;
Timer timer; //used to change window if time runs out
TimerTask timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_duck_screen);

    pref = getApplicationContext().getSharedPreferences("MY_PREF",MODE_PRIVATE);
    editor=pref.edit();


    duck_score=pref.getInt("tempDuckScore",0);

    timer = new Timer();
    timerTask = new TimerTask() {
        @Override
        public void run() {
            int tempDuckScore = duck_score;
            editor.putInt("tempDuckScore",tempDuckScore);

            timer.cancel();

            //below are my attempts to clear up memory space to make 
            //the app perform better.

            intent = new Intent(DuckScreen.this,GameOver.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    };



    timer.schedule(timerTask,2000);
}

// 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){
    timer.cancel();
    if (isLlamaScreen){
        Random r = new Random();
        screen = r.nextInt(2);
        if (screen == 1){ //DUCK
            int tempDuckScore = duck_score;
            editor.putInt("tempDuckScore",tempDuckScore);
            intent = new Intent(DuckScreen.this,DuckScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        } else{
            int tempDuckScore = duck_score;
            editor.putInt("tempDuckScore",tempDuckScore);
            intent = new Intent(DuckScreen.this,LlamaScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    }else{
        int tempDuckScore = duck_score;
        editor.putInt("tempDuckScore",tempDuckScore);
        intent = new Intent(DuckScreen.this,GameOver.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        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){
    timer.cancel();
    if (!isLlamaScreen){
        Random r = new Random();
        screen = r.nextInt(2);
        if (screen == 1){ //DUCK
            int tempDuckScore = duck_score;
            editor.putInt("tempDuckScore",tempDuckScore);
            intent = new Intent(DuckScreen.this,DuckScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        } else{
            int tempDuckScore = duck_score;
            editor.putInt("tempDuckScore",tempDuckScore);
            intent = new Intent(DuckScreen.this,LlamaScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
    }else{
        int tempDuckScore = duck_score;
        editor.putInt("tempDuckScore",tempDuckScore);
        intent = new Intent(DuckScreen.this,GameOver.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
}

public int getDuckScore(){
    return duck_score;
}

}

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="40sp"
        android:textColor="#ffffff"
        android:onClick="duckClick"
        />
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Llama"
        android:textSize="40sp"
        android:textColor="#ffffff"
        android:onClick="llamaClick"
        />
</RadioGroup>

</RelativeLayout>

错误味精:

04-05 11:57:59.451 6438-6438/com.example.ryan.llamaorduck E/dalvikvm-heap: Out of memory on a 8748016-byte allocation.
04-05 11:57:59.601 6438-6438/com.example.ryan.llamaorduck E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            java.lang.OutOfMemoryError
                                                                                at android.graphics.Bitmap.nativeCreate(Native Method)
                                                                                at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
                                                                                at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
                                                                                at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
                                                                                at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
                                                                                at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
                                                                                at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
                                                                                at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
                                                                                at android.content.res.Resources.loadDrawable(Resources.java:1940)
                                                                                at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
                                                                                at android.widget.ImageView.<init>(ImageView.java:119)
                                                                                at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
                                                                                at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
                                                                                at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                                at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
                                                                                at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1031)
                                                                                at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:668)
                                                                                at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                                at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                                                at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                                                at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                at com.example.ryan.llamaorduck.DuckScreen.onCreate(DuckScreen.java:28)
                                                                                at android.app.Activity.performCreate(Activity.java:4466)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
                                                                                at android.app.ActivityThread.access$600(ActivityThread.java:123)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                at android.os.Looper.loop(Looper.java:137)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:4424)
                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
                                                                                at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

尝试设置

android:largeHeap="true"

在您的清单的<application>标记内;这将启动应用程序,并将更多内存分配给堆。

Android Docs

注意:启用此功能也不能保证可用内存的固定增加,因为某些设备受其总可用内存的限制。

如果你开始使用OOM,你的代码就会遇到根本问题 - 这只是一个绑定。