AppCompatButton android:onClick找不到方法异常 [英] AppCompatButton android:onClick Could not find a method exception

查看:96
本文介绍了AppCompatButton android:onClick找不到方法异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只在KitKat版本上遇到这个问题,其余的从16个API级别到25个都可以正常工作

I have this problem just on KitKat version, the rest from 16 API level to 25 works fine

实例化布局的类具有布局的方法,例如

the class that instantiates the layout have the method of the layout, example

<android.support.v7.widget.AppCompatButton
  android:onClick="onClick"
...
public void onClick(View v) {
  // do something
}

我知道我可以将其更改为侦听器,数据绑定或使用诸如Butterknife之类的库,但是我有兴趣知道为什么只是在4.X版本上崩溃?

I know I can change this to listeners, databindings or use some library like Butterknife, but I'm interested in know why just crash on 4.X versions?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="onClick" />

    <android.support.v7.widget.AppCompatButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="onClick" />
</LinearLayout>

推荐答案

我已经找到了有关此问题的一些解释.它被归档为错误.这是错误报告的链接. https://issuetracker.google.com/issues/37108938

I have found some explanation for this issue. It was filed as a bug. Here is the link to bug report. https://issuetracker.google.com/issues/37108938

要点是,他们说此问题已在API 24中解决,并且onClick与支持库不兼容.我已在有关kitkat问题的主题中添加了评论.

In a gist, they say that the issue was fixed in API 24 and onClick is not compatible with support library. I have added a comment to the thread about kitkat issue.

这篇关于AppCompatButton android:onClick找不到方法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆