Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 取消生物特征认证时的祝酒问题_Android_Kotlin - Fatal编程技术网

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变量?你可以考虑传递你正在使用的活动的上下文。