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.
Could not find method in a parent or ancestor Context
That was terrible one. I've spent a lot of time on this, but the answer was simple. Always care about onCreate
method and actually setContentView(R.layout.);
line. I've been copy-pasting and tried to create classes with the same layout.
How do I solve Could not find method in a parent or ancestor Context for android:onClick attribute?
android:onClick="startService"
will not called in fragment. You should use OnClickListener
instead to handle this type of events.
Another approach is declare the startService
function in activity, then call the fragment method from the activity.
Kindly refer Android app crashing (fragment and xml onclick) to know more.
Could not find method method in a parent or ancestor Context for android:onClick attribute defined on view class MaterialButton with id id
The answer to my problem was to just set up the calls programmatically vs in layout for fragments. Using a setOnClickListener with the binding in onCreateView worked. If you are using a binding, make sure to return binding.root in the onCreateView, or else it won't work.
Could not find method in a parent or ancestor Context for android:onClick attribute
You must have "openHelp1
" method in same activity class where you use xlm file with attribute android:onClick="openHelp1"
You can't call method from another activity in this way.
Android studio : Could not find method XXXXX(View) in a parent or ancestor Context for android:onClick attribute defined on view class
With certitude, the called method by the onclick
must be on the activity.java
which launches the fragment.java
even if you are in this fragment.
But in this case in the design tool
, in the onclick
field, your method does not appear.
You need to add and define android:onClick
manually.
It remains a bit unclear but it works fine even if I probably do not use the right method.
How to solve error: Could not find method onClick(View) in a parent or ancestor Context for android:onClick
I had the same problem and in my case, I changed Button
in XML to android.support.v7.widget.AppCompatButton
and it worked.
Code with Error:
<Button
.... />
Fixed Code:
<android.support.v7.widget.AppCompatButton
.... />
Related Topics
Reading a Jsp Variable from JavaScript
How Does the Fetchmode Work in Spring Data JPA
How to Instantiate Non Static Inner Class Within a Static Method
Boolean[] VS. Bitset: Which Is More Efficient
How to Start a New Android Activity Using Class Name in a String
How to Get Country Phone Prefix from Iso
I Cant See Any Firebaserecycleradapter Items on My Layout
Method Called After Release() Exception Unable to Resume with Android Camera
Attempt to Re-Open an Already-Closed Object: SQLitedatabase
How to Print to the Console in Android Studio
How to Make Edittext Not Focused When Creating Activity
Sqliteopenhelper Failing to Call Oncreate
Error: Gradle: Execution Failed for Task ':App:Predexdebug'
Java Client and a C++ Server Send and Receive via Tcp Socket