java 如何解决错误:在 android:onClick 的父级或祖先上下文中找不到方法 onClick(View)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37136574/
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-11-03 02:14:08  来源:igfitidea点击:

How to solve error: Could not find method onClick(View) in a parent or ancestor Context for android:onClick

javaandroidxml

提问by Peter

I have seen that there's been some similar questions but the answers to those haven't helped me so far. The full error:

我已经看到有一些类似的问题,但到目前为止,这些问题的答案对我没有帮助。完整的错误:

java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_random'

java.lang.IllegalStateException:无法在父级或祖先上下文中为 android:onClick 属性找到方法 onClick(View) 在视图类 android.support.v7.widget.AppCompatButton 上定义,id 为“button_random”

The class (StartActivity.java):

类(StartActivity.java):

public class StartActivity extends AppCompatActivity {

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

    public void onClick(View v) {
        Log.d("DEBUG", "CLICKED " + v.getId());
    }

}

The XML (activity_start.xml):

XML ( activity_start.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Random Game"
        android:id="@+id/button_random"
        android:layout_gravity="center_horizontal"
        android:onClick="onClick" />
</LinearLayout>

I have added the activity to the AndroidManifest.xml. I have similar activities that work in the same way and I don't have any problems with those...

我已将活动添加到 AndroidManifest.xml。我有类似的活动,它们的工作方式相同,而且我对这些活动没有任何问题......

Does anyone see something where I am missing something or have made a mistake?

有没有人看到我遗漏了什么或犯了错误?

采纳答案by Alex Jolig

I had the same problem and in my case, I changed Buttonin XML to android.support.v7.widget.AppCompatButtonand it worked.

我遇到了同样的问题,在我的情况下,我将ButtonXML更改为android.support.v7.widget.AppCompatButton并且它起作用了。

Code with Error:

错误代码:

 <Button
        .... />

Fixed Code:

固定代码:

 <android.support.v7.widget.AppCompatButton
        .... />

回答by kgundula

Please rename onClickmethod to anything other than that, Android thinks you are calling the internal onClickfrom View.javaexposed via OnClickListenerinterface

请重新命名onClick方法比其他任何事情,Android的认为你是在调用内部onClickView.java通过暴露OnClickListener接口

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Random Game"
    android:id="@+id/button_random"
    android:layout_gravity="center_horizontal"
    android:onClick="myOnClick" />

and in your Activity

并在您的活动中

public class StartActivity extends AppCompatActivity {

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

        public void myOnClick(View v) {
          Log.d("DEBUG", "CLICKED " + v.getId());
        }
}

You can check the View documentation here

您可以在此处查看查看文档

回答by Muhammad Waleed

You need to change android.support.v7.widget.AppCompatButton

你需要改变 android.support.v7.widget.AppCompatButton

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Random Game"
        android:id="@+id/button_random"
        android:layout_gravity="center_horizontal"
        android:onClick="onClick" />
</LinearLayout>

回答by Ali Adil

I had the same issue, in my case I have 2 activities using the same layout so when I changed the OnClickevent name it will crash in other activity try to check the setContentViewlayout in the crashing activity

我遇到了同样的问题,在我的情况下,我有 2 个活动使用相同的布局,所以当我更改OnClick事件名称时,它会在其他活动中崩溃尝试检查setContentView崩溃活动中的布局