无法通过xml定义在活动中找到onClick方法 [英] Cannot find onClick method in activity from xml definition

查看:111
本文介绍了无法通过xml定义在活动中找到onClick方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将函数链接到AndroidStudio中按钮的onClick属性,但是由于某些原因,系统无法识别我编码的方法.

I am trying to link a function to the onClick property of a button in AndroidStudio but for some reason the system cannot identify the method that I coded.

有趣的是,当我用Java编写代码时,它可以正常工作.在科特林,事实并非如此.我更新了Kotlin并检查了其配置,但找不到问题.我的朋友尝试了与我在Linux计算机上所做的相同的操作,并且对他有用.我有Mac OS,但我不知道是否需要做一些额外的配置.谁能帮我吗?

The funny thing is that it works correctly when I code it in Java. In Kotlin it does not. I updated my Kotlin and checked its configuration but I cannot find the problem. My friend tried the same thing I did in a Linux computer and it worked for him. I have a Mac OS, I don't know if there is some extra configuration to take care of. Can anyone help me?

这是我的简单活动:

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun sendMessage(view: View) {

    }
}

这是xml:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:onClick="sendMessage"
    android:text="@string/send_button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/editText"
    app:layout_constraintTop_toTopOf="parent" />

在build.gradle中,我有:

In build.gradle I have:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

错误消息:

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

The onClick attribute value should be the name of a method in this View's context to invoke when the view is clicked.

This name must correspond to a public method that takes exactly one parameter of type View.  Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character

推荐答案

现在可以使用.我必须重新启动AndroidStudio.我仍然无法使用设计"选项卡中的属性"窗口添加功能,但是如果直接编辑XML,它就可以使用.

It works now. I had to restart AndroidStudio. I still cannot add the function using the attributes window in the design tab but it works if I edit the XML directly.

这篇关于无法通过xml定义在活动中找到onClick方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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