在我的 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/