【问题标题】:How to solve error: Could not find method onClick(View) in a parent or ancestor Context for android:onClick如何解决错误:在 android:onClick 的父上下文或祖先上下文中找不到方法 onClick(View)
【发布时间】:2016-05-10 10:59:51
【问题描述】:

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

java.lang.IllegalStateException: 找不到方法 onClick(View) 在 android:onClick 属性的父或祖先上下文中定义 在具有 id 的视图类 android.support.v7.widget.AppCompatButton 'button_random'

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

}

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>

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

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

【问题讨论】:

  • 您的错误消息是指AppCompatButton。你没有使用AppCompatActivity
  • 我在发布之前尝试了一些东西,抱歉。我已经编辑了上面的代码以反映我当前的代码。顺便说一句,这两个选项都不起作用。
  • 您的错误仍然显示 AppCompatButton,但您的问题不包括一个
  • 是的 cricket_007,这是正确的,也是我感到困惑的部分原因......

标签: java android xml


【解决方案1】:

就我而言,我错过了最后的右括号。

android:onClick="@{(v) -> CommentHandler.selectGallery(v)".

It should be like this.
        android:onClick="@{(v) -> CommentHandler.selectGallery(v)}".

【讨论】:

  • 兄弟,你拯救了我的一天
  • 谢谢。有人会认为 AS 会显示错误。
【解决方案2】:

请将 onClick 方法重命名为除此之外的任何其他名称,Android 认为您正在从通过 OnClickListener 接口暴露的 View.java 调用内部 onClick

<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" />

在你的活动中

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

您可以查看查看文档here

【讨论】:

    【解决方案3】:

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

    有错误的代码:

     <Button
            .... />
    

    固定代码:

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

    【讨论】:

    • 不,今天没有工作,我已经在使用AppCompatButton
    【解决方案4】:

    您需要更改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>
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

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