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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38392359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:16:29  来源:igfitidea点击:

Could not find method in parent or ancestor context

javaandroidillegalstateexception

提问by McLemore

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.

我一直在处理这个问题,并查看了我能找到的所有相关问题,例如:this onethis onethis 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'

Relevant code:

相关代码:

radio.java

电台.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

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>

采纳答案by Janki Gadhiya

Defining onClickin xmlmeans you need to define it for a particular view here is ImageButtonyou can not have two arguments in that method.

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

Your error is also saying that Could not find method playPauseMusic(View)means compiler needs a methods with single parameter Viewwhere as you were having two parameters View& ImageButtonthis is the reason why you where getting that error. Just remove one argument from the method and it will work.

您的错误还说找不到方法 playPauseMusic(View)意味着编译器需要一个具有单个参数的方法View,因为您有两个参数ViewImageButton这就是您收到该错误的原因。只需从方法中删除一个参数,它就会起作用。

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 playPauseMusicwill be called on Button click so you have already defined a button click so no need to define it inside the methodby playPause.setOnClickListenerso I have removed that code.

另一件事写作android:onClick="playPauseMusic"意味着该方法playPauseMusic将在按钮单击时调用,因此您已经定义了一个按钮单击,因此无需在方法内部定义它playPause.setOnClickListener因此我删除了该代码。

回答by johnrao07

Your code possibly should start with:

您的代码可能应该以:

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

You're specifying onClick in xml

您在 xml 中指定 onClick

android:onClick="playPauseMusic"

So, the method works, you've got inner onClicks too. If they are some views.

因此,该方法有效,您也有内部 onClicks。如果他们有一些看法。

You gotta initialize and get it from the xml in code, for ex-

您必须初始化并从代码中的 xml 中获取它,例如-

If you have ImageButton in xml, whose id is "playPause"

如果您在 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
        }
    });
}

In Your case, you've got onClick attribute in xml and another onCLick in code. You Use one.

在您的情况下,您在 xml 中有 onClick 属性,在代码中有另一个 onCLick 属性。你用一个。