【问题标题】:java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view classjava.lang.IllegalStateException:在视图类上定义的 android:onClick 属性的父或祖先上下文中找不到方法 count(View)
【发布时间】:2021-04-06 19:56:38
【问题描述】:

每当我点击计数或重置按钮时,应用程序都会崩溃 我认为该应用程序无法识别 onclick 方法计数和重置 我尝试使用 java 进行相同的操作,但使用 java 工作正常,所以问题一定出在 kotlin 这个问题有什么解决方案吗?还是我应该采用其他方法?

package com.example.a39kotlinfun

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView

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


    var textView = findViewById<TextView>(R.id.textView)

    var count = 0

    fun count(view : View){
        count += 1
        textView.text = count.toString()
    }

    fun reset(view : View){
        count = 0
        textView.text = "0"
    }
}

}

XML:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_marginBottom="400dp"
    android:text="Hello World!"
    android:textSize="36sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="55dp"
    android:layout_marginLeft="55dp"
    android:layout_marginTop="111dp"
    android:layout_marginBottom="247dp"
    android:onClick="count"
    android:text="count"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="111dp"
    android:layout_marginEnd="51dp"
    android:layout_marginRight="51dp"
    android:layout_marginBottom="247dp"
    android:onClick="reset"
    android:text="reset"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

Logcat:

java.lang.IllegalStateException: Could not find method count(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'button'
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)
    at android.view.View.performClick(View.java:7189)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
    at android.view.View.performClickInternal(View.java:7166)
    at android.view.View.access$3500(View.java:819)
    at android.view.View$PerformClick.run(View.java:27682)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7592)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

【问题讨论】:

    标签: java android kotlin developer-tools


    【解决方案1】:

    countreset 方法移出onCreate 方法。

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
           super.onCreate(savedInstanceState)
           setContentView(R.layout.activity_main)
    
           var textView = findViewById<TextView>(R.id.textView)
        }
    
        var count = 0
    
        fun count(view : View){
            count += 1
            textView.text = count.toString()
        }
    
        fun reset(view : View){
            count = 0
            textView.text = "0"
        }
    }
    

    在任何情况下都应避免设置android:onClick,最好的解决方案是使用:

    val button = findViewById<Button>(R.id.button)
    button.setOnClickListener {
        count += 1
        textView.text = count.toString()
    }
    

    【讨论】:

    • 谢谢你,这有效。但我不明白为什么我们必须使用 setOnClickListener 方法而不是 onClick
    • @AdityaVSM 尝试使用 android:onClick 和 Fragments。
    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多