如何修复Android中指定为非空的参数为空错误

uwopmtnx  于 2023-02-14  发布在  Android
关注(0)|答案(2)|浏览(146)

在我的应用程序中,我应该实现MultiDex依赖项,我将其添加到gradle. build文件中,并将一些代码写入applicationclass!
但运行应用程序后,显示强制关闭错误和未启动应用程序!

    • Gradle.构建版本:**
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:multidex-instrumentation:1.0.3'
    • 基本活动类:**
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))
    }
}
    • 应用程序类:**
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()
        )
    }
    • 错误消息:**
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)

显示此行的错误:import androidx.multidex.MultiDex
我如何解决这个问题?

zdwk9cvp

zdwk9cvp1#

尝试在App类中扩展MultiDexApplication(),而不是Application()

    • 编辑**

好吧,因为答案是在同一个答案,但在评论中,我会更新这个答案,以防其他人需要它:
再试试这个:

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

代替

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

如果你在我的评论中看到捆绑标记有"?"

v440hwme

v440hwme2#

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

此处的savedInstanceState参数也应该可以为空(Bundle?

相关问题