我试图将一个函数链接到 AndroidStudio 中按钮的 onClick 属性,但由于某种原因,系统无法识别我编码的方法。
有趣的是,当我用 Java 编写代码时,它可以正常工作。在 Kotlin 中则不然。我更新了 Kotlin 并检查了其配置,但找不到问题。我的朋友在 Linux 计算机上尝试了我所做的同样的事情,结果对他有用。我有一个 Mac 操作系统,我不知道是否需要一些额外的配置。谁能帮我?
这是我的简单活动:
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 中我有:
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
尝试添加
tools:context="com.somepackage.MainActivity"
到你的 xml 顶视图
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)