gc释放了xxxx对象而不是冻结应用程序

时间:2014-12-20 13:10:16

标签: garbage-collection freeze

我正在尝试创建包含十个活动的简单应用。每个活动看起来几乎相同,它有四个按钮(不同颜色),当点击一个特定按钮时,它打开下一个活动。每个活动的OnCreate方法都有播放该活动名称的媒体播放器。过了一段时间后,我在LogCat中看到gc释放了一些物体,当时Activity不播放任何声音,按钮被禁用。

你有什么建议或建议如何解决这个问题?

以下是一个活动的代码:

public class Green extends Activity {

int buttonActive = 0;
int buttonWrong = 0;
MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_green);

    buttonWrong = 0;
    playSound();

    Button btn = (Button)findViewById(R.id.button1);
    Button btn2 = (Button)findViewById(R.id.button2);
    Button btn3 = (Button)findViewById(R.id.button3);
    Button btn4 = (Button)findViewById(R.id.button4);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(buttonActive == 1){

            playSoundCorrect();
            Intent intent = new Intent(v.getContext(),Blue.class);
            startActivity(intent);
            }
        }
    });

    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(buttonActive == 1 && buttonWrong == 0){
                buttonWrong = 1;
                playSoundWrong();
            }           
        }
    });

btn3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(buttonActive == 1 && buttonWrong == 0){
                buttonWrong = 1;
                playSoundWrong();
            }           
        }
    });

btn4.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if(buttonActive == 1 && buttonWrong == 0){
            buttonWrong = 1;

            playSoundWrong();
        }       
    }
});

}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}


private void playSound(){

    MediaPlayer player = MediaPlayer.create(this, R.raw.green);
    player.start(); 
    player.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
            buttonActive = 1;
        }
    });
}

private void playSoundWrong(){

player = MediaPlayer.create(this, R.raw.wrong2);

if(!player.isPlaying()){
    player.start();

}

player.setOnCompletionListener(new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
        buttonWrong = 0;
    }
});

}

private void playSoundCorrect(){

MediaPlayer player = MediaPlayer.create(this, R.raw.correct);
player.start();
player.setOnCompletionListener(new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
    }
});
}
}

1 个答案:

答案 0 :(得分:0)

private void playSound中,您正在屏蔽班级的player属性。如果您说player = MediaPlayer.create,那么类的player属性将被设置为新的MediaPlayer,只要该类可以访问,该播放器就可以访问。相反,您正在说MediaPlayer player = MediaPlayer.create,它正在创建一个范围为player方法的新playSound变量 - 一旦方法终止,player将无法再访问,并且被垃圾收集,即使班级仍然可以到达。