java - 修复 Android Studio/Kotlin 中单击按钮时出现的错误?

标签 java android android-studio kotlin error-handling

在我的 Android 应用程序中,每次单击按钮时都会出现错误。

Kotlin :

fun goClicked(view: View) {
    mAuth.signInWithEmailAndPassword(emailEditText?.text.toString(), passwordEditText?.text.toString())
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                logIn()
            } else {
                // If sign in fails, display a message to the user.
                Toast.makeText(this, "Account nicht gefunden!",
                    Toast.LENGTH_SHORT).show()

            }
        }
}

XML:

<Button
    android:id="@+id/button10"
    style="@android:style/Widget.Holo.Button"
    android:layout_width="244dp"
    android:layout_height="61dp"
    android:layout_marginStart="83dp"
    android:layout_marginTop="115dp"
    android:layout_marginEnd="84dp"
    android:layout_marginBottom="215dp"
    android:background="@drawable/roundedbutton"
    android:onClick="goClicked"
    android:text="Login"
    android:textColor="#F8000000"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/passwordEditText" />

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: spiels.dev.easysender, PID: 31395
    java.lang.IllegalStateException: Could not find method goClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button3'
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
        at android.view.View.performClick(View.java:7352)
        at android.widget.TextView.performClick(TextView.java:14177)
    at android.view.View.performClickInternal(View.java:7318)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27807)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7037)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

每次我单击按钮时都会显示此错误消息。

希望你能帮助我 最好的问候桑德罗

最佳答案

看起来您正在另一个按钮中调用相同的函数

id:'button3'

因此,当您单击 goClicked 函数时,Android 会搜索所有引用。需要它。

TL;DR. Change the name of your function goClicked and the issue should be solved.

关于java - 修复 Android Studio/Kotlin 中单击按钮时出现的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454054/

相关文章:

android - 获取注册的电子邮件帐户

android - 在 Core 2 Duo 10.11.6 Mac OS 上运行 Android Studio 模拟器

java - 同一对象上的同步块(synchronized block)是否真的阻止其他线程执行该 block ?

java - Spring MVC : Correct Annotation of Controller Method for RESTful URI including ';'

java - 创建一个新框架并使用 SpringUtilities 使用 SpringLayout 对其进行装饰

android - Android中的MultiLine EditText,光标从顶部开始

android - Android 中的 USB 网络摄像头

android - (AMD 使用 Windows 和 Android Studio)错误 : x86 emulation currently requires hardware acceleration

android - 如何使用 Kotlin 使用振动器效果在单击按钮时振动 Android 设备?

java - 如何将对象插入到h2