【问题标题】:Could not find method in parent or ancestor context在父上下文或祖先上下文中找不到方法
【发布时间】:2016-11-18 10:37:19
【问题描述】:

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

相关代码:

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>

【问题讨论】:

  • 这就是radio_activity中的所有内容
  • 我能看到你调用这个方法playPauseMusic的代码吗? 当用户触摸按钮时调用我能看到那个按钮代码吗??
  • 它在radio_activity。这是 ImageButton。

标签: java android illegalstateexception


【解决方案1】:

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

您的错误还说 Could not find method playPauseMusic(View) 意味着编译器需要具有单个参数 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所以我删除了那个代码。

【讨论】:

    【解决方案2】:

    您的代码可能应以:

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

    【讨论】:

      【解决方案3】:

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

      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)

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

      按照这里的建议 Android Studio not deploying changes to app

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-07
        • 1970-01-01
        相关资源
        最近更新 更多