You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何初始化lateinit变量?Android开源应用运行报错求助

Fixing lateinit property textInput has not been initialized Error

Hey there! Let's get that error sorted out for you. The issue here is exactly what the message says: you've declared textInput as a lateinit variable, but you're trying to use it (like when setting up the EditingRecyclerAdapter or MarkdownRecyclerAdapter) before giving it a value.

What's lateinit anyway?

In Kotlin, lateinit tells the compiler: "Trust me, I'll assign a value to this variable before anyone tries to use it". But right now, you're breaking that promise—so the app crashes when it tries to access textInput.

How to fix this

You have two straightforward options to initialize textInput properly:

Option 1: Initialize it in onCreate()

Since you're using an Activity, the onCreate() method is the perfect place to set up your variables before the UI starts interacting with them. Here's how to update your code:

First, make sure you override onCreate() in your MainActivity (if you don't already have it):

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.your_main_activity_layout) // Replace with your actual layout ID
    
    // Initialize textInput here—use the SurvivalContent instance you already have
    // Adjust this based on what methods SurvivalContent provides
    // For example, if it has a method to get text content as a list:
    textInput = survivalContent.getTargetTextData().toMutableList()
    
    // Any other setup code you need goes here
}

Just replace getTargetTextData() with the actual method from SurvivalContent that returns the text content you need for the adapters.

Option 2: Use lazy initialization instead of lateinit

Since you're already using lazy for other variables (like drawerToggle and survivalContent), you can do the same for textInput. This way, it will automatically initialize the first time you use it, so you don't have to worry about timing:

// Replace the lateinit line with this:
private val textInput by lazy {
    // Use the correct method from SurvivalContent to get your data
    survivalContent.getTargetTextData().toMutableList()
}

This works perfectly because survivalContent is also a lazy variable—so it will be initialized right before textInput needs it, no extra setup required.

Why git submodule update didn't help

That command updates any submodules your project depends on, but this error is about your app's code logic (not missing dependencies). So updating submodules wouldn't fix an uninitialized variable issue.

Quick check

Whichever option you choose, make sure that textInput is initialized before any code tries to use it. In your current code, the isInEditMode observable uses textInput when switching adapters—so as long as textInput is set up before isInEditMode changes (which it will be with either option above), you're good to go.

内容的提问来源于stack exchange,提问作者Yashwin.L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:05:32