Could not find method in parent or ancestor context

I dealt with this problem for some time and looked through all the relevant questions that I could find, for example: this , this , and this . Could you help me fix this error? This is the only one trap throws.

java.lang.IllegalStateException: Could not find method playPauseMusic(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'playPause'

Relevant Code:

radio.java

 package com.example.jacob.wutk; import android.media.AudioManager; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import java.io.IOException; public class radio extends AppCompatActivity { /** Called when the user touches the button */ public void playPauseMusic (View view, final ImageButton playPause) throws IOException { 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(); } }); playPause.setOnClickListener(new View.OnClickListener(){ public void onClick(View view) { if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); playPause.setImageResource(R.drawable.play1); } else { 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); } } 

activity_radio.xml

  <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" tools:context="com.example.jacob.wutk.radio"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left|center_vertical" android:scaleType="centerCrop" android:src="@drawable/background_mic1"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingBottom="1.0dip" android:paddingLeft="4.0dip" android:paddingRight="4.0dip" android:paddingTop="5.0dip"> <ImageButton android:id="@+id/playPause" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_weight="1.0" android:background="?android:selectableItemBackground" android:clickable="true" android:onClick="playPauseMusic" android:scaleType="fitCenter" android:src="@drawable/play1"/> <ImageView android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_marginRight="5dp" android:layout_weight="1.0" android:background="?android:selectableItemBackground" android:scaleType="fitCenter" android:src="@drawable/logo"/> </LinearLayout> </FrameLayout> 
+8
java android illegalstateexception
source share
2 answers

Defining onClick in xml means that you need to define it for a specific view here ImageButton you cannot have two arguments in this method.

Your error also says that Unable to find the playPauseMusic (View) method means that the compiler needs methods with a single View parameter, where you had two View parameters and ImageButton this is the reason you get this 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); } } 

Another thing writing android:onClick="playPauseMusic" means that the playPauseMusic method will be called when the button is clicked, so you already defined the button, so you don’t need to define it inside the playPause.setOnClickListener method playPause.setOnClickListener so I deleted this code.

+4
source share

Perhaps your code should start with:

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

You specify onClick in xml

 android:onClick="playPauseMusic" 

So the method works, you have an internal onClicks. If these are some submissions.

You need to initialize and get it from xml in code, for ex -

If you have an ImageButton in xml whose identifier is "playPause"

 ImageButton playPause; //Declare it here if you wanna use it in all other places in the class or outside of your class @Override protected 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 have the onClick attribute in xml and other onCLick code. You use it.

+1
source share

All Articles