Android 取消生物特征认证时的祝酒问题
当用户取消提示时,我在创建祝酒词时遇到一些问题 我得到一个错误:Android 取消生物特征认证时的祝酒问题,android,kotlin,Android,Kotlin,当用户取消提示时,我在创建祝酒词时遇到一些问题 我得到一个错误: java.lang.RuntimeException:无法在未调用Looper.prepare()的线程上toast 以下是我的代码,用于受影响的区域: object : BiometricPrompt.AuthenticationCallback() { override fun onAuthenticationError(errorCode: Int, errString
java.lang.RuntimeException:无法在未调用Looper.prepare()的线程上toast
以下是我的代码,用于受影响的区域:
object : BiometricPrompt.AuthenticationCallback()
{
override fun onAuthenticationError(errorCode: Int, errString: CharSequence?) {
super.onAuthenticationError(errorCode, errString)
Toast.makeText(applicationContext, "Authentication Error. Please try again :)", Toast.LENGTH_LONG)
.show()
}
// onAuthSucceeded would be here.
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Toast.makeText(applicationContext, "Authentication Failed. Please try again :)", Toast.LENGTH_LONG)
.show()
}
}
我已经尝试在Toast.makeText之前添加Looper.prepare(),但没有帮助
提前感谢您的帮助:)发生这种情况是因为您正在工作线程上调用toast
您可以使用下面的代码在主线程上运行它
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Authentication Error. Please try again :)", Toast.LENGTH_SHORT).show();
}
});
Activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Authentication Error. Please try again :)", Toast.LENGTH_SHORT).show();
}
});
发生此问题是因为上面的toast在工作线程上被调用
您可以使用下面的代码在主线程上运行它
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Authentication Error. Please try again :)", Toast.LENGTH_SHORT).show();
}
});
Activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Authentication Error. Please try again :)", Toast.LENGTH_SHORT).show();
}
});
您必须在UI线程内调用Toast
。在Kotlin中,代码如下所示
runOnUiThread(
object : Runnable {
override fun run() {
Toast.makeText(applicationContext, "Authentication Error. Please try again :)", Toast.LENGTH_LONG)
.show()
}
}
)
如何初始化applicationContext变量?你可以考虑传递你正在使用的活动的上下文。