Could not find method in parent or ancestor context Could not find method in parent or ancestor context android android

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.


Your code possibly should start with:

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

You're specifying onClick in xml

android:onClick="playPauseMusic"

So, the method works, you've got inner onClicks too. If they are some views.

You gotta initialize and get it from the xml in code, for ex-

If you have ImageButton in xml, whose id is "playPause"

ImageButton playPause; //Declare it here if you wanna use it in all other places in the class or outside of your class@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_radio);    playPause = (ImageButton)findViewById(R.id.playPause);    playPause.setOnClickListener(new View.OnClickListener(){        public void onClick(View view) {           //OnCLick Stuff        }    });}

In Your case, you've got onClick attribute in xml and another onCLick in code. You Use one.


The question is already answered but I hope this will help someone like me in the future. I was having this error:

E/AndroidRuntime: FATAL EXCEPTION: main    Process: com.example.test1, PID: 6892    java.lang.IllegalStateException: Could not find method btnClickStartThread(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'btnStartThread1'        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)

After struggling a lot I got it fixed by going to layouts and I realized I had two layouts:

  1. activity_main.xml
  2. activity_main.xml (v21)

My changes were just reflecting on one and the app was loading other version of layout.

As suggested hereAndroid Studio not deploying changes to app