当前位置: 首页 > 知识库问答 >
问题:

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

郑波
2023-03-14

我已经处理这个问题有一段时间了,并查看了我能找到的所有相关问题,例如:这个问题,这个问题和这个问题。你能帮我纠正这个错误吗?这是唯一一个被圆木扔出去的。

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);
    }
}

无线电活动。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>

共有3个答案

高和通
2023-03-14

这个问题已经得到了回答,但我希望这将有助于像我这样的人在未来。我犯了这个错误:

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)

挣扎了很多后,我得到了修复去布局,我意识到我有两个布局:

  1. activity\u main。xml
  2. activity\u main。xml(v21)

我的更改只是反映在一个应用程序上,而应用程序正在加载另一个版本的布局。

在这里建议Android Studio不部署应用程序的更改

丁业
2023-03-14

您的代码可能应该以以下内容开头:

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

您正在xml中指定onClick

android:onClick="playPauseMusic"

所以,这个方法是有效的,你也有内部的onclick。如果他们有一些观点。

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

如果你在xml中有ImageButton,它的id是"playPence"

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

卫建义
2023-03-14

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

您的错误还表示找不到方法playPauseMusic(View)意味着编译器需要一个具有单个参数View的公共方法,而您有两个参数:View

这就是为什么你会犯这样的错误。只要从方法中删除一个参数,它就会工作。

这样做:

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因此我删除了该代码。

 类似资料:
  • 问题内容: 我处理这个问题已有一段时间了,研究了我可以找到的所有相关问题,例如:这个,这个和这个。您能帮我解决这个错误吗?这是logcat抛出的唯一一个。 相关代码: radio.java activity_radio.xml 问题答案: 定义在您需要定义它特定视图方式在这里你不能在该方法两个参数。 您的错误还表示 无法找到方法playPauseMusic(View) 意味着编译器需要 带有单个参

  • 问题内容: 我已经看到有一些类似的问题,但是到目前为止,这些问题的答案并没有帮助我。完整错误: java.lang.IllegalStateException:在视图类android.support.v7.widget.AppCompatButton上定义的ID为“ button_random”的android:onClick属性中,在父级或祖先上下文中找不到onClick(View)方法 类(

  • 正在执行热重启。..正在将文件同步到设备sdk gphone x86 arm...在1,910毫秒内重新启动应用程序。 widgets library捕获异常widgets library捕获异常widgets library捕获异常widgets library捕获异常widgets library捕获异常widgets library捕获异常widgets library捕获异常widgets

  • 问题内容: 我正在尝试从dataTable内部的graphicImage更新一个dataTable。我尝试了很多f:ajax渲染值的组合,但是都没有成功,现在我使用的是PrimeFaces的p:component函数,但是之前遇到了相同的错误。我在浏览器中收到此错误: f:ajax包含未知ID’j_idt690:painelTabelaAPDespesa’-无法在组件j_idt735的上下文中找到

  • 问题内容: 我有一个如下的二叉树。我需要找到最不常见的祖先(LCA)。例如6和4的LCA为1,4和5的LCA为2。 谁能建议我该如何解决这个问题? 问题答案: 从普通的深度优先搜索算法开始: 现在,将其修改为采用两个“目标”参数,即target1和target2。 当搜索target1带您离开,而搜索target2带您去时,您已经找到了LCA。 假设两个目标确实存在。如果需要断言它们确实如此,则需

  • 问题内容: 我知道这是在黑暗中拍摄的,但是有没有办法仅使用CSS,CSS2,没有jquery,没有javascript来选择和设置元素祖先的样式?我已经遍历了选择器,但是发布了它,以防万一我错过了一些东西或者有一个聪明的解决方法。 例如,假设我有一个嵌套在div中的类名称为“ test”的表。是否有某种: 问题答案: CSS2或CSS3中没有父选择器之类的东西。实际上,可能永远不会存在,因为一旦开