找不到对应的方法处理程序-Android XML [英] Corresponding method handler not found - Android XML

查看:66
本文介绍了找不到对应的方法处理程序-Android XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建对现有项目的XML进行更改时遇到了此错误.这就是我的XML的样子

I came across this error when creating making changes to the XML of an existing project. This is what my XML looked like

activity_month_view.xml

activity_month_view.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.YearViewActivity">

    <ImageView
        android:id="@+id/month_view_prev_month"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_keyboard_arrow_left_black_24dp"
        android:onClick="changeMonth"/>

</android.support.design.widget.CoordinatorLayout>

我的Java课

MonthViewActivity.java

MonthViewActivity.java

public class MonthViewActivity extends AppCompatActivity {

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

    public void changeMonth(View view) {
        //Some methods here
    }

}

onClick 属性显示此错误

 Corresponding method handler 'public void changeMonth(android.view.View)' not found

我查看了

I had a look at this and this but they did not help.

推荐答案

在XML中声明上下文时,我犯了一个错误.我曾经使用过 .activities.YearViewActivity ,但是 changeMonth 方法是在另一个活动中声明的,这导致了错误.

I had made a mistake in declaring the context in the XML. I had used .activities.YearViewActivity but the changeMonth method was declared in a different activity, which was causing the error.

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MonthViewActivity"> // Changed here

    <ImageView
        android:id="@+id/month_view_prev_month"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_keyboard_arrow_left_black_24dp"
        android:onClick="changeMonth"/>

</android.support.design.widget.CoordinatorLayout>

tools:context 更改为调用活动的类可以为我解决该错误.

Changing the tools:context to the class of the calling activity fixed the error for me.

这篇关于找不到对应的方法处理程序-Android XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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