android-branch-deep-linking-attribution icon indicating copy to clipboard operation
android-branch-deep-linking-attribution copied to clipboard

NullPointerException at onActivityResumed

Open dogan-gulcan-zocdoc opened this issue 4 years ago • 1 comments

We are experiencing NullPointerException and we are unable to reproduce on our end.

Crash report

Caused by java.lang.NullPointerException: Attempt to invoke interface method 'void io.branch.referral.Branch$BranchReferralInitListener.a(org.json.JSONObject, io.branch.referral.BranchError)' on a null object reference
       at io.branch.referral.Branch$InitSessionBuilder.init(Branch.java:3227)
       at io.branch.referral.BranchActivityLifecycleObserver.onActivityResumed(BranchActivityLifecycleObserver.java:82)
       at android.app.Application.dispatchActivityResumed(Application.java:455)
       at android.app.Activity.dispatchActivityResumed(Activity.java:1291)
       at android.app.Activity.onResume(Activity.java:1819)
       at androidx.fragment.app.FragmentActivity.onResume(FragmentActivity.java:455)
       at com.xyz.android.base.BaseActivity.onResume(BaseActivity.java:93)
       at com.xyz.android.activity.WebViewActivity.onResume(WebViewActivity.java:294)
       at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454)
       at android.app.Activity.performResume(Activity.java:8105)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4529)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4572)
       at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
       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:2220)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:8016)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)

Below you can find implementation details;

Application.kt

 override fun onCreate() {
    super.onCreate()
    Branch.getAutoInstance(this).disableTracking(true)
    ..
}

StarterActivity.kt -- Launcher activity

    override fun onStart() {
         super.onStart()
         initBranchSession(isAllowedToTrack())
    }


    private fun initBranchSession(allowedToTrack: Boolean) {
        if (allowedToTrack) {
            try {
                val callback: (referringParams: JSONObject?, error: BranchError?) -> Unit = { referringParams, error ->
                    try {
                        if (error == null) {
                            val branchUri = BranchLinkUtils.extractParametersIntoUrl(referringParams!!)
                            if (branchUri != null) {
                                overrideIntentUrl(branchUri)
                            }
                        }
                    } catch (ex: Exception) {
                        ...
                    }
                }
                if (Branch.bypassCurrentActivityIntentState()) {
                    branchDisableBypassCurrentActivityIntentState()
                }
                if (Branch.getInstance() == null) {
                    Branch.sessionBuilder(this)
                        .withCallback(callback)
                        .withData(this.intent.data)
                        .init()
                } else {
                    intent.putExtra("branch_force_new_session", true)

                    Branch.sessionBuilder(this)
                        .withCallback(callback)
                        .withData(this.intent.data)
                        .reInit()
                }

            } catch (e: Exception) {
                ...
            }
        } else if (!allowedToTrack) {
            Branch.enableBypassCurrentActivityIntentState()
            val builder = Branch.sessionBuilder(this)
            if (Branch.getInstance() == null) {
                builder.init()
            } else {
                builder.reInit()
            }
        }
    }

AnalyticsManager.kt We are disabling/enabling tracking depending on the screen user is in on Activity.onStart() like below;

    ...
    fun setAllowedLogging(isEnabled: Boolean) {
         if (!isEnabled) {
                Branch.disableInstantDeepLinking(true)
                Branch.setPlayStoreReferrerCheckTimeout(Long.MAX_VALUE)
                Branch.getAutoInstance(context.applicationContext).disableTracking(true)
            } else {
                Branch.disableInstantDeepLinking(false)
                Branch.setPlayStoreReferrerCheckTimeout(10)
                Branch.getAutoInstance(context.applicationContext).disableTracking(false)
            }
    }
    ...

build.gradle

dependencies {
  implementation "io.branch.sdk.android:library:5.0.1"
  ..
}

dogan-gulcan-zocdoc avatar May 11 '20 17:05 dogan-gulcan-zocdoc

Hey @dogan-gulcan-zocdoc,

Wanted to follow up and see if you were still experiencing this issue.

jf-branch avatar Oct 17 '21 09:10 jf-branch