javaandroidkotlinandroid-multidex

How to fix Parameter specified as non-null is null error in Android


In my application i should implement MultiDex dependency, and i added this in gradle.build file and write some codes into application class!
But after run application, show me force close error and not started application!
gradle.build :

implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:multidex-instrumentation:1.0.3'

BaseActivity class :

abstract class BaseActivity : AppCompatActivity() {
    /**
     * Set your page layout when initialize `layoutId`, Ex. R.layout.activity_main
     */
    abstract var layoutId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId)
    }

    /**
     * With below method, we can set custom font to all of activities
     */
    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase))
    }
}

Application class :

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.multidex.MultiDex
import com.adjust.sdk.Adjust
import com.adjust.sdk.AdjustConfig
import com.adjust.sdk.LogLevel
import com.app.android.R
import com.app.android.utils.ADJUST_APP_TOKEN
import com.crashlytics.android.Crashlytics
import com.crashlytics.android.answers.Answers
import com.crashlytics.android.answers.ContentViewEvent
import com.google.android.gms.analytics.GoogleAnalytics
import com.google.android.gms.analytics.HitBuilders
import com.google.android.gms.analytics.Tracker
import io.fabric.sdk.android.Fabric
import io.github.inflationx.calligraphy3.CalligraphyConfig
import io.github.inflationx.calligraphy3.CalligraphyInterceptor
import io.github.inflationx.viewpump.ViewPump

class App : Application() {

    private var adjustEnvironment: String = ""
    private lateinit var adjustConfig: AdjustConfig
    private lateinit var analytics: GoogleAnalytics
    private lateinit var tracker: Tracker

    override fun onCreate() {
        super.onCreate()
        //Calligraphy
        ViewPump.init(
            ViewPump.builder().addInterceptor(
                CalligraphyInterceptor(
                    CalligraphyConfig.Builder().setDefaultFontPath("fonts/IranSans.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
                )
            ).build()
        )
        //GoodPres
        GoodPrefs.init(applicationContext)
        //Fabric initialize kits
        val fabric = Fabric.Builder(this)
            .kits(Answers())
//            .kits(Crashlytics())
            .debuggable(true)
            .build()
        Fabric.with(fabric)
        //Adjust
        adjustEnvironment = AdjustConfig.ENVIRONMENT_PRODUCTION
        adjustConfig = AdjustConfig(this, ADJUST_APP_TOKEN, adjustEnvironment, true)
        adjustConfig.setSendInBackground(true)
        adjustConfig.setLogLevel(LogLevel.SUPRESS)
        Adjust.onCreate(adjustConfig)
        registerActivityLifecycleCallbacks(AdjustLifecycleCallbacks())
        //Google Analytics
        analytics = GoogleAnalytics.getInstance(this)
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    //Adjust lifecycle method
    private class AdjustLifecycleCallbacks : Application.ActivityLifecycleCallbacks {
        override fun onActivityResumed(activity: Activity) {
            Adjust.onResume()
        }

        override fun onActivityPaused(activity: Activity) {
            Adjust.onPause()
        }

        override fun onActivityStopped(activity: Activity) {}

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}

        override fun onActivityDestroyed(activity: Activity) {}

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle) {}

        override fun onActivityStarted(activity: Activity) {}
    }

    @Synchronized
    fun getDefaultTracker(): Tracker {
        if (tracker == null) {
            tracker = analytics.newTracker(R.xml.global_tracker)
        }
        return tracker
    }

    fun anserTracker(name: String, type: String, id: String) {
        Answers.getInstance().logContentView(
            ContentViewEvent()
                .putContentName(name)
                .putContentType(type)
                .putContentId(id)
        )
    }

    fun googleTracker(category: String, action: String, label: String) {
        val tracker: Tracker = getDefaultTracker()
        tracker.send(
            HitBuilders.EventBuilder()
                .setCategory(category)
                .setAction(action)
                .setLabel(label)
                .build()
        )
    }

Error message :

Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState
    at com.app.android.ui.App$AdjustLifecycleCallbacks.onActivityCreated(Unknown Source:7)
    at android.app.Application.dispatchActivityCreated(Application.java:221)
    at android.app.Activity.onCreate(Activity.java:1071)
    at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:147)
    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:317)
    at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:106)
    at com.app.android.ui.base.BaseActivity.onCreate(BaseActivity.kt:16)

Show me error for this line : import androidx.multidex.MultiDex

How can i fix this issue?


Solution

  • Try extending the MultiDexApplication() in your App class instead of Application()

    Edit

    Ok since the answer is in the same answer but in the comment , I would update this answer in case others need it :

    And also try this :

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {}
    

    instead of

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
    

    if you see the bundle in my comment is marked with "?"