0

I am having some issues creating a Toast when a BiometricPrompt is canceled by the user.

I am getting the error:

java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()

Here is my code for the area that this is affecting:

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()
                }
            }

I have tried adding Looper.prepare() before Toast.makeText, but that doesnt help.

Thanks in advance for your help :)

Sxribe
  • 819
  • 7
  • 16
  • How do you initialize the applicationContext variable? You may consider passing the context of activity you're using it in. – Jakub Kostka Oct 12 '19 at 19:07
  • Possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](https://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – Roaim Oct 12 '19 at 19:30

4 Answers4

0

When you create you create BiometricPrompt with:

BiometricPrompt (activity, executor, callback)

you have to provide an executor, you can get executor like this:

val executor = ContextCompat.getMainExecutor (activity)

But if you use for example:

val executor = Executors.newFixedThreadPool (100)

the callback onBiometricAuthenticationError will be invoked in a Thread other than the main Thread and the Toast cannot be shown if not in the mainThread

vitiello.antonio
  • 323
  • 3
  • 12
-1

This is happening because you are calling toast on worker thread,

you can use below code to run it on main thread.

activity.runOnUiThread(new Runnable() {
  public void run() {
    Toast.makeText(activity, "Authentication Error. Please try again :)", Toast.LENGTH_SHORT).show();
  }
});
-1

This issue is happening because above toast is getting called on worker thread,

you can use below code to run it run on main thread.

Activity.runOnUiThread(new Runnable() {
  public void run() {
    Toast.makeText(activity, "Authentication Error. Please try again :)", Toast.LENGTH_SHORT).show();
  }
});
Rahul
  • 1,380
  • 1
  • 10
  • 24
-1

You must call Toast inside UI thread. In Kotlin it would be like below code

runOnUiThread(
        object : Runnable {
            override fun run() {
                Toast.makeText(applicationContext, "Authentication Error. Please try again :)", Toast.LENGTH_LONG)
                        .show()
            }
        }
)
Firdavs Khodzhiev
  • 336
  • 2
  • 4
  • 18