简体   繁体   English

Android Studio“从未使用分配给MediaPlayer的null值”

[英]Android Studio “The value null assigned to MediaPlayer is never used”

I'm having a problem in Android Studio. 我在Android Studio中遇到问题。 I am making a sound board and I want to make my MediaPlayer null when it has finished playing. 我正在制作一个音板,当它播放完毕后,我想将我的MediaPlayer设为null。 I have created an on complete listener but inside the listener when I make my MediaPlayer equal to null, the studio states a warning that "The value null assigned to "mp" is never used." 我已经创建了一个完整监听器,但是当我使MediaPlayer等于null时,在监听器内部,工作室警告说“从未使用分配给“ mp”的null值“。

public void playUpToUs (View view) {
    if (mp == null){
        mp = MediaPlayer.create(this, R.raw.its_up_to_us);
        mp.start();
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
                mp = null;
            }
        });

    } else {
        mp.release();
        mp=null;
    }
}

I also have four other methods just like this except that they play a different sound on click. 除此以外,我还有其他四种方法,它们在单击时会播放不同的声音。 I am probably making an obvious mistake but I would really appreciate if someone could help me. 我可能犯了一个明显的错误,但是如果有人可以帮助我,我将不胜感激。

This is because your mp parameter in the onCompletion method is shadowing the mp member variable that you're trying to modify. 这是因为onCompletion方法中的mp参数遮盖了您要修改的mp成员变量。 You're actually setting the parameter to null, which is useless here. 您实际上是将参数设置为null,在这里没有用。 You want to modify the shadowed instance instead. 您想改为修改阴影实例。

Either use a different variable name that doesn't conflict with the member variable, or refer to the member using the outer class name as a qualifier (such as MyActivity.this.mp , if your the outer class is MyActivity). 请使用与成员变量不冲突的其他变量名,或者使用外部类名作为限定符来引用成员(例如,如果外部类为MyActivity.this.mp ,则使用MyActivity.this.mp)。

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

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