我一直在处理这个问题,并查看了我能找到的所有相关问题,例如:this one,this one和this one。你能帮我纠正这个错误吗?它是logcat抛出的唯一一个。
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'
相关代码:
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>
答案 0 :(得分:7)
在onClick
中定义xml
意味着你需要为特定视图定义它ImageButton
你不能在该方法中有两个参数。
你的错误也说无法找到方法playPauseMusic(View)意味着编译器需要一个单参数View
的方法,因为你有两个参数View
&amp; ; ImageButton
这就是你在哪里得到那个错误的原因。只需从方法中删除一个参数即可。
这样做:
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);
}
}
编写android:onClick="playPauseMusic"
还有一件事意味着在点击按钮时会调用方法playPauseMusic
,因此您已经定义了一个按钮点击,因此无需在方法中定义按playPause.setOnClickListener
,我删除了该代码。
答案 1 :(得分:5)
您的代码可能应该以:
开头@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio);
}
您在xml中指定onClick
android:onClick="playPauseMusic"
所以,这个方法有效,你也有内在的onClicks。如果他们是一些观点。
你必须初始化并从代码中的xml中获取它,对于前 -
如果您在xml中有ImageButton,其id为“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
}
});
}
在您的情况下,您在xml中有onClick属性,在代码中有另一个onCLick。你用一个。
答案 2 :(得分:0)
问题已经回答了,但我希望这会在未来帮助像我这样的人。我遇到了这个错误:
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)
苦苦挣扎后,我转到 layouts
解决了这个问题,我意识到我有两种布局:
activity_main.xml
activity_main.xml (v21)
我的更改只是反映在一个上,而应用程序正在加载其他版本的布局。