简体   繁体   中英

How to pass more than one argument?

I am working on Android Studio 3.1.3.
My application consists of eight buttons. I am trying to pass more than one button as arguments from onClick method

public void onClick(View v) {

    switch (v.getId()){

        case R.id.button:
            pauseAudio(audioTwo, audioThree, audioFour, audioFive, audioSix, audioSeven, audioEight);
            mediaPlayer = MediaPlayer.create(this,R.raw.birds);
            mediaPlayer.start();
            break;

to pauseAudio method,

public void pauseAudio(View view, Button audioOne, Button audioTwo, Button audioThree, Button audioFour, Button audioFive, Button audioSix, Button audioSeven, Button audioEight){

        if(audioOne.isEnabled()
                || audioTwo.isEnabled()
                || audioThree.isEnabled()
                || audioFour.isEnabled()
                || audioFive.isEnabled()
                || audioSix.isEnabled()
                || audioSeven.isEnabled()
                || audioEight.isEnabled()){
            if(mediaPlayer.isPlaying() && mediaPlayer!=null){
                mediaPlayer.stop();
            }
        }

    }

this is error it is showing when I hover over the underlined text 当我将鼠标悬停在带下划线的文本上时,这是显示的错误
I am guessing the way I am passing argument is not the correct way, so please help me out. Thank you in advance.

To make your code easier and hopefully work, try this:

public void pauseAudio(View... views) {
    for(View view : views) {
        if(view.isEnabled()) {
            if(mediaPlayer!=null && mediaPlayer.isPlaying()){
                mediaPlayer.stop();
            }
            break;
        }
    }
}

Now you can call it like this:

pauseAudio(audioOne, audioTwo, audioThree, ...);

EDIT:

Maybe also check if the view is not initialized:

if(view != null && view.isEnabled())

EDIT 2:

I'm glad to hear that it worked :) Here's a better explanation:

public void pauseAudio(View... views) { //Allow a dynamic initialization of an array of Views
    for(View view : views) { //Loop through the array
        if(view != null && view.isEnabled()) { //If the current view is not null and enabled
            if(mediaPlayer!=null && mediaPlayer.isPlaying()){ //If the MediaPlayer is not null and playing
                mediaPlayer.stop(); //Stop the MediaPlayer
            }
            break; //Break the loop, as we have reached our usecase
        }
    }
}

Maybe if you publish the log after crash we could help you better. but I already see a control that can crash

  if (mediaPlayer.isPlaying () && mediaPlayer! = null) {
            mediaPlayer.stop ();
        }

. you could first check if mediaPlayer! = null before mediaPlayer.isPlaying () like this:

   if (mediaPlayer! = null && mediaPlayer.isPlaying ()) {
            mediaPlayer.stop ();
        }

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