在我的应用程序中,我使用的是 androidx 生物识别支持库 1.0.1。在 Android 9 上我可以看到这种错误,但我不知道它们来自哪里::
02-01 13:00:06.422 23937 23937 E AndroidRuntime: FATAL EXCEPTION: main
02-01 13:00:06.422 23937 23937 E AndroidRuntime: Process: xxxxxxx, PID: 23871
02-01 13:00:06.422 23937 23937 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.hardware.fingerprint.FingerprintManager.getErrorString(int, int)' on a null object reference
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.hardware.biometrics.BiometricPrompt.lambda$sendError$0(BiometricPrompt.java:490)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.hardware.biometrics.-$$Lambda$BiometricPrompt$HqBGXtBUWNc-v8NoHYsj2gLfaRw.run(Unknown Source:6)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6672)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
02-01 13:00:06.422 23937 23937 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
代码:
KeyguardManager km = (KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE);
BiometricPrompt.Builder builder = new BiometricPrompt.Builder(getContext().getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.setConfirmationRequired(true);
if (km != null && km.isDeviceSecure()) {
builder.setDeviceCredentialAllowed(true);
} else {
builder.setNegativeButton(getContext().getResources().getString(android.R.string.cancel), getContext().getApplicationContext().getMainExecutor(), (dialog, which) -> {
//something
});
}
} else {
builder.setNegativeButton(getContext().getResources().getString(android.R.string.cancel), getContext().getApplicationContext().getMainExecutor(), (dialog, which) -> {
//something
});
}
builder.setDescription(getContext().getResources().getString(R.string.confirm_pin));
builder.setTitle(getContext().getResources().getString(R.string.modify_pin));
BiometricPrompt prompt = builder.build();
prompt.authenticate(new CancellationSignal(), getContext().getMainExecutor(), new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
}
@Override
public void onAuthenticationFailed() {
}
});