2

这是它的样子。我希望视觉可以帮助您帮助我!

在此处输入图像描述

我已经在三个设备和四个具有不同屏幕尺寸(不同的图像尺寸)和 Android 版本的模拟器上对其进行了测试,并且我没有遇到任何内存不足的错误。我让我 3 岁的孩子反复敲打动物,疯狂地翻动它们,但仍然没有 OOM。

根据Google Play的数据,自 2013 年 9 月 3 日以来,已安装了近 200 次。到目前为止,我只收到了一份关于 OOM 错误的报告,但现在我担心它们会开始滚动。我包括整个活动,以防我的重写方法之一具有相关信息或类似信息。这是我第一个发布的应用程序,所以请原谅任何新手错误并随时批评任何内容(只要您包含一些纠正它的建议!)

Game2Show 中的 setImages 方法(包括第 235 行,'int currentAnimalImage...'):

private void setImages() {
    //Get current letter and switch in name, letter and image that corresponds.
    getLetter();
    String l_img = "letter_" + currentLetter;
    String a_img = "animal_" + currentLetter;
    String n_img = "name_" + currentLetter;

    int currentLetterImage = getResources().getIdentifier(l_img, "drawable", this.getPackageName());
    ImageView l_iv = (ImageView) findViewById(R.id.current_letter);
    l_iv.setImageResource(currentLetterImage);

    int currentAnimalImage = getResources().getIdentifier(a_img, "drawable", this.getPackageName());
    ImageView a_iv = (ImageView) findViewById(R.id.animal_image);
    a_iv.setImageResource(currentAnimalImage);

    int currentAnimalName = getResources().getIdentifier(n_img, "drawable", this.getPackageName());
    ImageView n_iv = (ImageView) findViewById(R.id.animal_name);
    n_iv.setImageResource(currentAnimalName);
    Log.v(TAG, "voice: " + voice);
    Log.v(TAG, "Config.alphabetSoundsArray_voice1: " + Config.alphabetSoundsArray_voice1);
    Log.v(TAG, "Config.alphabetSoundsArray_voice2: " + Config.alphabetSoundsArray_voice2);
    Log.v(TAG, "currentIndex: " + currentIndex);

    if (Config.alphabetSoundsArray_voice1[currentIndex] !=0){
        if (voice == 1) {
            Sound(Config.alphabetSoundsArray_voice1[currentIndex]);
        }
    }
    else {
    }
        if (Config.alphabetSoundsArray_voice2[currentIndex] !=0){
            if (voice == 2) {
                Sound(Config.alphabetSoundsArray_voice2[currentIndex]);
            }
        }
        else {
        }
        System.gc();
    }

堆栈跟踪:

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:1937)
at android.content.res.Resources.getDrawable(Resources.java:664)
at android.widget.ImageView.resolveUri(ImageView.java:542)
at android.widget.ImageView.setImageResource(ImageView.java:315)
at com.zenlifegames.teachtryabc123.Game2Show.setImages(Game2Show.java:235)
at com.zenlifegames.teachtryabc123.Game2Show.onCreate(Game2Show.java:57)
at android.app.Activity.performCreate(Activity.java:4465)
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)

游戏2秀:

package com.zenlifegames.teachtryabc123;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Game2Show extends Activity {
    private TextView textViewa;
    private ImageView homeClicked;
    int voice;
    Handler handler = new Handler();
    Intent i;
    int currentIndex = 1;
    String currentLetter;
    ArrayList<Integer> alphabetNums;
    String TAG = "Game2Show: ";

    //private final String TAG = "Game2Show: ";
    private int minAlphaRange = 1;
    private int maxAlphaRange = 26;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game2_show);
        i = new Intent(Game2Show.this, MyMusicService.class);
        getMyViews();
        setupPrefs();
        Config.getPrefs(this);
        musicToggleCheck();
        userPreferences();
        setFont();
        setupAlphabetArray();
        setImages();
        Toast.makeText(Game2Show.this,
                       "Touch the letters, animals and words to hear audio.",
                       Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Intent settingsActivity = new Intent(getBaseContext(),
        Preferences.class);
        startActivity(settingsActivity);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, GameSelector.class);
        startActivity(intent);
        super.onBackPressed();
    }

    public void returnHome(View view) {
        homeClicked.setImageResource(R.drawable.homebuttonclicked);
        if (Config.soundArray[0] !=0){Sound(Config.soundArray[0]);}
        Intent intent = new Intent(this, GameSelector.class);
        startActivity(intent);
    }

    private void musicToggleCheck() {
        ImageButton musicToggleButton =(ImageButton)findViewById(R.id.music_toggle);
        if (isMyServiceRunning()) {
            musicToggleButton.setImageResource(R.drawable.music_toggle);
        }
        else {
            musicToggleButton.setImageResource(R.drawable.music_toggle_off);
        }
    }

    private void getMyViews() {
        TextView textViewa = (TextView)findViewById(R.id.childs_name);
        this.textViewa = textViewa;
        final ImageView homeClicked = (ImageView) findViewById(R.id.home);
        this.homeClicked = homeClicked;
    }

    private void setFont() {
        Typeface t = Typeface.createFromAsset(this.getAssets(), "fonts/pen.ttf");
        textViewa.setTypeface(t);
         int[] color = {Color.RED, Color.BLUE};
         float[] position = {0, 1};
         TileMode tile_mode = TileMode.MIRROR; // Or TileMode.REPEAT;
         LinearGradient lin_grad = new LinearGradient(0, 0, 0, 50,color,position, tile_mode);
         Shader shader_gradient = lin_grad;
         textViewa.getPaint().setShader(shader_gradient);
    }

    private void userPreferences() {
        //Store childs name
        textViewa.setText(Config.childsNameString);

        //Set voice choice
        int getVoiceChoice = Integer.parseInt(Config.storeVoiceChoice);

        if (getVoiceChoice == 1) {
            int voice = 1;    //Mom
            this.voice = voice;
        }
        else {
            int voice = 2;    //yg
            this.voice = voice;
        }
        //Log.v(TAG, "voice: " + voice);
    }

    private void setupAlphabetArray() {
        alphabetNums = new ArrayList<Integer>(maxAlphaRange);
        for(currentIndex = minAlphaRange; currentIndex <= maxAlphaRange; currentIndex++) {
            alphabetNums.add(currentIndex);
        }
    }

    private void getLetter(){
        if (currentIndex < minAlphaRange)
            currentIndex = maxAlphaRange;
        if (currentIndex > maxAlphaRange)
            currentIndex = minAlphaRange;

        if (currentIndex == 1) currentLetter = "a";
        if (currentIndex == 2) currentLetter = "b";
        if (currentIndex == 3) currentLetter = "c";
        if (currentIndex == 4) currentLetter = "d";
        if (currentIndex == 5) currentLetter = "e";
        if (currentIndex == 6) currentLetter = "f";
        if (currentIndex == 7) currentLetter = "g";
        if (currentIndex == 8) currentLetter = "h";
        if (currentIndex == 9) currentLetter = "i";
        if (currentIndex == 10) currentLetter = "j";
        if (currentIndex == 11) currentLetter = "k";
        if (currentIndex == 12) currentLetter = "l";
        if (currentIndex == 13) currentLetter = "m";
        if (currentIndex == 14) currentLetter = "n";
        if (currentIndex == 15) currentLetter = "o";
        if (currentIndex == 16) currentLetter = "p";
        if (currentIndex == 17) currentLetter = "q";
        if (currentIndex == 18) currentLetter = "r";
        if (currentIndex == 19) currentLetter = "s";
        if (currentIndex == 20) currentLetter = "t";
        if (currentIndex == 21) currentLetter = "u";
        if (currentIndex == 22) currentLetter = "v";
        if (currentIndex == 23) currentLetter = "w";
        if (currentIndex == 24) currentLetter = "x";
        if (currentIndex == 25) currentLetter = "y";
        if (currentIndex == 26) currentLetter = "z";
    }

    public void goBack(View view) {
        if (Config.soundArray[0] !=0){
            Sound(Config.soundArray[0]);
        }
        currentIndex--;
        setImages();
    }

    public void goForward(View view) {
        if (Config.soundArray[0] !=0){
            Sound(Config.soundArray[0]);
        }
        currentIndex++;
        setImages();
    }

    public void sayLetter(View view) {
        if (voice == 1) {
            if (Config.alphabetSoundsArray_voice1[currentIndex] !=0){
                Sound(Config.alphabetSoundsArray_voice1[currentIndex]);
            }
        }
        if (voice == 2) {
            if (Config.alphabetSoundsArray_voice2[currentIndex] !=0){
                Sound(Config.alphabetSoundsArray_voice2[currentIndex]);
            }
        }
    }

    public void sayAnimal(View v) {
        if (voice == 1) {
            if (Config.animalNamesArray_voice1[currentIndex] !=0){
                Sound(Config.animalNamesArray_voice1[currentIndex]);
            }
        }
        if (voice == 2) {
            if (Config.animalNamesArray_voice2[currentIndex] !=0){
                Sound(Config.animalNamesArray_voice2[currentIndex]);
            }
        }
    }

    public void animalNoise(View v) {
        if (Config.animalSoundsArray[currentIndex] !=0){
            Sound(Config.animalSoundsArray[currentIndex]);
        }
    }

    public void sideLetterClicked(View v) {
        int id = v.getId();
        if (id == R.id.side_letter_1)
            currentIndex=1;

        else if (id == R.id.side_letter_2) currentIndex=2;
        else if (id == R.id.side_letter_3) currentIndex=3;
        else if (id == R.id.side_letter_4) currentIndex=4;
        else if (id == R.id.side_letter_5) currentIndex=5;
        else if (id == R.id.side_letter_6) currentIndex=6;
        else if (id == R.id.side_letter_7) currentIndex=7;
        else if (id == R.id.side_letter_8) currentIndex=8;
        else if (id == R.id.side_letter_9) currentIndex=9;
        else if (id == R.id.side_letter_10) currentIndex=10;
        else if (id == R.id.side_letter_11) currentIndex=11;
        else if (id == R.id.side_letter_12) currentIndex=12;
        else if (id == R.id.side_letter_13) currentIndex=13;
        else if (id == R.id.side_letter_14) currentIndex=14;
        else if (id == R.id.side_letter_15) currentIndex=15;
        else if (id == R.id.side_letter_16) currentIndex=16;
        else if (id == R.id.side_letter_17) currentIndex=17;
        else if (id == R.id.side_letter_18) currentIndex=18;
        else if (id == R.id.side_letter_19) currentIndex=19;
        else if (id == R.id.side_letter_20) currentIndex=20;
        else if (id == R.id.side_letter_21) currentIndex=21;
        else if (id == R.id.side_letter_22) currentIndex=22;
        else if (id == R.id.side_letter_23) currentIndex=23;
        else if (id == R.id.side_letter_24) currentIndex=24;
        else if (id == R.id.side_letter_25) currentIndex=25;
        else if (id == R.id.side_letter_26) currentIndex=26;

        else {
        }
        setImages();
    }

    private void setImages() {
        //Get current letter and switch in name, letter and image that corresponds.
        getLetter();
        String l_img = "letter_" + currentLetter;
        String a_img = "animal_" + currentLetter;
        String n_img = "name_" + currentLetter;

        int currentLetterImage = getResources().getIdentifier(l_img, "drawable", this.getPackageName());
        ImageView l_iv = (ImageView) findViewById(R.id.current_letter);
        l_iv.setImageResource(currentLetterImage);

        int currentAnimalImage = getResources().getIdentifier(a_img, "drawable", this.getPackageName());
        ImageView a_iv = (ImageView) findViewById(R.id.animal_image);
        a_iv.setImageResource(currentAnimalImage);

        int currentAnimalName = getResources().getIdentifier(n_img, "drawable", this.getPackageName());
        ImageView n_iv = (ImageView) findViewById(R.id.animal_name);
        n_iv.setImageResource(currentAnimalName);
        Log.v(TAG, "voice: " + voice);
        Log.v(TAG, "Config.alphabetSoundsArray_voice1: " + Config.alphabetSoundsArray_voice1);
        Log.v(TAG, "Config.alphabetSoundsArray_voice2: " + Config.alphabetSoundsArray_voice2);
        Log.v(TAG, "currentIndex: " + currentIndex);

        if (Config.alphabetSoundsArray_voice1[currentIndex] !=0){
            if (voice == 1) {
                Sound(Config.alphabetSoundsArray_voice1[currentIndex]);
            }
        }
        else {
        }

        if (Config.alphabetSoundsArray_voice2[currentIndex] !=0){
            if (voice == 2) {
                Sound(Config.alphabetSoundsArray_voice2[currentIndex]);
            }
        }
        else {
        }
        System.gc();
    }

    //new
    @Override
    protected void onPause() {
        super.onPause();
        //Implemented to force music to stop when home is pressed.
        Context context = getApplicationContext();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        if (!taskInfo.isEmpty()) {
            ComponentName topActivity = taskInfo.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                if(isMyServiceRunning()){
                    stopService(i);
                }
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

    }

    public void setupPrefs() {
        ImageButton settingsClicked = ((ImageButton) findViewById(R.id.prefButton));
        settingsClicked.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ImageView settingsClicked = ((ImageView) findViewById(R.id.prefButton));
                settingsClicked.setImageResource(R.drawable.settings_button_clicked);
                MediaPlayer buttonClicked = MediaPlayer.create(Game2Show.this, R.raw.click);
                buttonClicked.start();

                Intent settingsActivity = new Intent(getBaseContext(),
                                                     Preferences.class);
                startActivity(settingsActivity);
            }
        });
    }

    public boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (MyMusicService.class.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    public void musicToggle(View v) {
        final ImageButton musicToggleButton =(ImageButton)findViewById(R.id.music_toggle);
        if ((!isMyServiceRunning())) {
            startService(i);
            Toast.makeText(Game2Show.this, "Music on.", Toast.LENGTH_SHORT).show();
            musicToggleButton.setImageResource(R.drawable.music_toggle);
        }
        else {
            stopService(i);
            Toast.makeText(Game2Show.this, "Music off.", Toast.LENGTH_SHORT).show();
            musicToggleButton.setImageResource(R.drawable.music_toggle_off);
        }
    }

    public void Sound(int s){
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        Config.spool.play(s, volume, volume, 1, 0, 1f);
    };

    @Override
    protected void onResume() {
        Log.v(TAG, "onResume called.");
        super.onResume();
        if (Config.spool != null) {
        }
        else {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
    }
}
4

0 回答 0