How to get value from bundle without causing Warning ClassCastException: android.net.Uri$StringUri cannot be cast to java.lang.String

Viewed 10

In my current Android project i have the following code to check the contents of the intent extras passed to my Activity

override fun onResume() {
    super.onResume()

    val bundle = intent.extras
    if (bundle != null) {
        for (key in bundle.keySet()) {
            if (key == Intent.EXTRA_REFERRER
                && !TextUtils.isEmpty(bundle.getString(key)) // FAILS HERE
                && bundle.getString(key)!!.startsWith("android-app://$packageName")
            ) referrerCount++
        }
    }

    if (isPromptLogin && referrerCount > 1) {
        cancelSignIn(EXTRA_VALUE_SIGN_IN_CANCELLED)
    }
}

the key value pair that is causing me an issue is the following:-

KEY = android.intent.extra.REFERRER
value = https://id.company.com/as/6LHhp/resume/as/authorization.ping?client_id=MY-APP-ANDROID&

my logcat has this warning

 Key android.intent.extra.REFERRER expected String but value was a android.net.Uri$StringUri.  The default value <null> was returned.
 Attempt to cast generated internal exception:
    java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.lang.String
    at android.os.BaseBundle.getString(BaseBundle.java:1374)
    at com.my.app.SignInActivity.onResume(SignInActivity.kt:72)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1531)
    at android.app.Activity.performResume(Activity.java:8422)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4793)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4836)
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7898)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

do i need to be concerned about this warning?

is there any approach i can take to refactor my code to stop this warning from being triggered?

0 Answers
Related