简体   繁体   中英

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

I'm having a problem in Android Studio. I am making a sound board and I want to make my MediaPlayer null when it has finished playing. 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."

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. You're actually setting the parameter to null, which is useless here. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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