Search code examples
androidkotlinandroid-jetpack-composeandroid-biometric-prompt

java.lang.ClassCastException: com.compose.MainActivity cannot be cast to androidx.fragment.app.FragmentActivity


I am trying to integrate BiometricPrompt with Jetpackcompose

Here is a snippet of my code which is called on a button click

val context = LocalContext.current
val biometricPrompt =BiometricPrompt(context as FragmentActivity,ContextCompat.getMainExecutor(context),object : BiometricPrompt.AuthenticationCallback() {.....

The error is context as FragmentActivity, not sure how to and what to cast the context as. I tried Activity, ComponentActivity as well.

As soon as i click the button i get error saying

java.lang.ClassCastException: com.compose.MainActivity cannot be cast to androidx.fragment.app.FragmentActivity

I tried looking in to this as well but it does not work


Solution

  • According to the doc, BiometricPrompt requires a FragmentActivity.

    enter image description here

    Check your MainActivity class.
    It should extend AppCompatActivity or FragmentActivity instead of ComponentActivity.