:kotlin.UninitializedPropertyAccessException:lateinit 属性管理器尚未初始化

Posted

技术标签:

【中文标题】:kotlin.UninitializedPropertyAccessException:lateinit 属性管理器尚未初始化【英文标题】:: kotlin.UninitializedPropertyAccessException: lateinit property manager has not been initialized 【发布时间】:2020-09-06 10:18:12 【问题描述】:

我正在开发新闻,我正在关注 nullpointexception

java.lang.RuntimeException: Unable to start activity ComponentInfoyodgorbek.komilov.musobaqayangiliklari/yodgorbek.komilov.musobaqayangiliklari.ui.WelcomeActivity: kotlin.UninitializedPropertyAccessException: lateinit property manager has not been initialized
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2976)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3113)
 at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
 at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
 at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858)
 at android.os.Handler.dispatchMessage(Handler.java:106)
 at android.os.Looper.loop(Looper.java:201)
 at android.app.ActivityThread.main(ActivityThread.java:6820)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property manager has not been initialized
 at yodgorbek.komilov.musobaqayangiliklari.ui.WelcomeActivity.onCreate(WelcomeActivity.kt:26)
 at android.app.Activity.performCreate(Activity.java:7224)
 at android.app.Activity.performCreate(Activity.java:7213)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2956)
 ... 11 more

在我的 WelcomeActivity.kt 类下面

class WelcomeActivity : AppIntro() 


    private  lateinit  var manager: PreferencesManager

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        // Make sure you don't call setContentView!

        if (manager.isFirstRun()) 
            showIntroSlides()
         else 
            goToMain()
        
    

        // Call addSlide passing your Fragments.

        // You can use AppIntroFragment to use a pre-built fragment

       private fun showIntroSlides() 
           manager.setFirstRun()
           addSlide(
                AppIntroFragment.newInstance(
                    title = "Welcome to the NewsApp",
                    description = "NewsApp give your information about life news around the world",
                    imageDrawable = R.drawable.news,
                    backgroundDrawable = R.drawable.news_slider,
                    titleColor = Color.YELLOW,
                    descriptionColor = Color.RED,
                    backgroundColor = Color.BLUE,
                    titleTypefaceFontRes = R.font.opensans_light,
                    descriptionTypefaceFontRes = R.font.opensans_regular
                )
            )
            addSlide(
                AppIntroFragment.newInstance(
                    title = "...Let's get started!",
                    description = "This is the last slide, I won't annoy you more :)"
                )
            )
        

    private fun goToMain() 
        startActivity(Intent(this, MainActivity::class.java))
    

    override fun onSkipPressed(currentFragment: Fragment?) 
        super.onSkipPressed(currentFragment)
      goToMain()


    

    override fun onDonePressed(currentFragment: Fragment?) 
        super.onDonePressed(currentFragment)
        goToMain()
    
    override fun onSlideChanged(oldFragment: Fragment?, newFragment: Fragment?) 
        super.onSlideChanged(oldFragment, newFragment)
        Log.d("Hello", "Changed")
    

即使我尝试按照以下方式初始化管理器,我也不明白导致空指针异常的原因

private var manager: PreferencesManager? = null 

但这并没有解决我的问题 我想知道我在哪里犯错了我必须做些什么来避免空指针异常

【问题讨论】:

【参考方案1】:

查看日志很简单。您的 manager 在使用前未初始化。看看onCreate

    private  lateinit  var manager: PreferencesManager

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        // Make sure you don't call setContentView!

        // <<<<<<<<<<<<<<< INITIALISE manager HERE >>>>>>>>>>>>>>

        if (manager.isFirstRun())  <<<<<<<< HERE you are using an uninitilised manager
            showIntroSlides()
         else 
            goToMain()
        
    

onCreate() 中的if (manager.isFirstRun()) 之前初始化manager

【讨论】:

好的,我明白了,我正在制作 nullpointer 异常,我必须做些什么才能避免 @sashabeliy 查看我的答案的更新。您需要初始化管理器。 lateinit 不创建对象。 var manager: PreferencesManager? = null // 确保你没有调用 setContentView! if (manager?.isFirstRun()!!) showIntroSlides() else goToMain() 像这样,但应用程序仍然崩溃 你知道如何创建 PreferencesManager 的对象吗? 如果我按照你所说的其他方法做我不能打电话给经理你建议根本不工作?

以上是关于:kotlin.UninitializedPropertyAccessException:lateinit 属性管理器尚未初始化的主要内容,如果未能解决你的问题,请参考以下文章