无法在父或祖先上下文中找到方法

Could not find method in parent or ancestor context

我处理这个问题已经有一段时间了,并且查看了我能找到的所有相关问题,例如:this one, this one, and 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>

您的代码可能应该以:

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

您在 xml

中指定了 onClick
android:onClick="playPauseMusic"

所以,该方法有效,您也有内部 onClicks。如果他们是一些看法。

你必须在代码中初始化并从 xml 中获取它,对于 ex-

如果你在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 属性。你用一个。

xml 中定义 onClick 意味着您需要在此处为​​特定视图定义它 ImageButton 您不能在该方法中有两个参数。

你的错误还说 Could not find method playPauseMusic(View) 意味着编译器需要一个 public 方法和单个参数 View,而你有两个参数:View & 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 所以我删除了那个代码。

问题已经得到解答,但我希望这对将来像我这样的人有所帮助。我遇到了这个错误:

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 解决了这个问题,我意识到我有两种布局:

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

我的更改只反映在一个版本上,应用正在加载其他版本的布局。

按照这里的建议