无法在父级或祖先上下文中找到方法 [英] Could not find method in parent or ancestor context
问题描述
我一直在处理这个问题,并查看了我能找到的所有相关问题,例如: this this , this , this this 。你能帮我纠正这个错误吗?它是logcat抛出的唯一一个。
I've been dealing with this problem for awhile and have looked at all the relevant questions I could find, such as: this one, this one, and this one. Could you help me correct this error? It's the only one being thrown by the 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>
推荐答案
定义 onClick
in xml
表示你需要为特定视图定义它是 ImageButton
你不能有两个该方法中的参数。
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.
你的错误也说找不到方法playPauseMusic(View)意味着编译器需要一个单参数的方法查看
您有两个参数的位置查看
& ImageButton
这就是您收到该错误的原因所在。只需从方法中删除一个参数即可。
Your error is also saying that Could not find method playPauseMusic(View) means compiler needs a methods with single parameter View
where as 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.
这样做:
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
在方法中定义它,所以我删除了该代码。
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.
这篇关于无法在父级或祖先上下文中找到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!