Could not find method in parent or ancestor context

Defining onClick in xml means you need to define it for a particular view here is ImageButton you can not have two arguments in that method.

Your error is also saying that Could not find method playPauseMusic(View) means compiler needs a public method with single parameter View, whereas you were having two parameters: View & ImageButton.

This is the reason why you where getting that error. Just remove one argument from the method and it will work.

Do it like this :

public class radio extends AppCompatActivity {

    /** Called when the user touches the button */

    public void playPauseMusic (View playPause) {
        String url = "http://streamer.cci.utk.edu:8000/wutk-vorbis"; // your URL here
        final MediaPlayer mediaPlayer = new MediaPlayer();

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer){
                mediaPlayer.start();
            }
        });

        
        if (mediaPlayer.isPlaying()) {
             mediaPlayer.pause();
             ((ImageButton)playPause).setImageResource(R.drawable.play1);
        } else {
            ((ImageButton)playPause).setImageResource(R.drawable.pause1);
        }
        
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepareAsync();
    }

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

One more thing writing android:onClick="playPauseMusic" means the method playPauseMusic will be called on Button click so you have already defined a button click so no need to define it inside the method by playPause.setOnClickListener so I have removed that code.

Leave a Comment