Jetpack's BiometricPrompt (androidx.biometric.BiometricPrompt) throws NullPointerException

China☆狼群 提交于 2019-12-11 17:24:22

问题


I'm using androidx.biometric:biometric:1.0.0-alpha03 in a test project and when pushing the negative button I always get the following error message:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence androidx.biometric.BiometricFragment.getNegativeButtonText()' on a null object reference
    at androidx.biometric.BiometricPrompt$1$1.run(BiometricPrompt.java:329)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)

I think this is a library issue which needs to be fixed as the fragment seems to be null or is there anything I'm doing wrong here?

val prompt = BiometricPrompt(activity, Executors.newSingleThreadExecutor(), object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {}
        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {}
        override fun onAuthenticationFailed() {}
})

val promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle(title)
        .setSubtitle(subtitle)
        .setDescription(description)
        .setNegativeButtonText(activity.getString(android.R.string.cancel))
        .build()

prompt.authenticate(promptInfo)

回答1:


This was reported three months ago as a bug, and it was reported as fixed two months ago. However, they have not yet released a fresh artifact.

There are some comments in that issue that suggest that "it's a timing thing", and that adding delays can help as a workaround.




回答2:


When looking at the line in question:

.setNegativeButtonText(activity.getString(android.R.string.cancel))

One can notice, that there are probably two arguments missing (it's even a whole other method):

.setNegativeButton("Cancel", executor, cancelListener)

And the code posted above does not feature any CancelListener ...

which seems likely to be the reason for that NullPointerException.

For reference: Better Biometrics in Android P & What is new in Android P — BiometricPrompt.



来源:https://stackoverflow.com/questions/55481789/jetpacks-biometricprompt-androidx-biometric-biometricprompt-throws-nullpointe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!