kotlin - 如何初始化lateinit变量?
问题描述
我正在尝试运行我收到错误的开源应用程序
lateinit property textInput has not been initialized
textInput
使用的主要代码
class MainActivity : BaseActivity() {
private val drawerToggle by lazy { ActionBarDrawerToggle(this, drawer_layout, drawer_open, drawer_close) }
private val survivalContent by lazy { SurvivalContent(assets) }
private lateinit var currentUrl: String
private lateinit var currentTopicName: String
private lateinit var textInput: MutableList<String>
private var lastFontSize = State.getFontSize()
private var lastNightMode = State.nightModeString()
private var lastAllowSelect = State.allowSelect()
private val linearLayoutManager by lazy { LinearLayoutManager(this) }
private var isInEditMode by observable(false, onChange = { _, _, newMode ->
if (newMode) {
fab.setImageResource(drawable.ic_image_remove_red_eye)
contentRecycler.adapter = EditingRecyclerAdapter(textInput)
} else {
fab.setImageResource(drawable.ic_editor_mode_edit)
contentRecycler.adapter = MarkdownRecyclerAdapter(textInput, imageWidth(), onURLClick)
}
contentRecycler.scrollToPosition(State.lastScrollPos)
})
因为我是 android 开发的新手,所以在使用它之前我不知道如何初始化 lateinit 变量。在此之前,我尝试按照这个githubgit submodule update
中提到的那样做,但它没有用。所以现在我希望问题出在初始化textInput变量。
解决方案
您必须textInput
在使用它之前初始化变量。为此,您可以执行以下操作:
textInput = mutableListOf("Abc", "Xyz")
来自文档:在初始化之前访问 lateinit 属性会引发一个特殊异常,该异常清楚地标识正在访问的属性以及它尚未初始化的事实。
阅读更多:https ://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables
推荐阅读
- php - 在 woo-commerce 默认电子邮件功能中添加修改
- sql-server - 两个月前的数据
- c# - 连接字符串的密码
- php - 单击按钮时,Codeigniter 不会将输入表单重定向到仪表板
- jquery - WooCommerce jQuery 在页面上工作,但不在相同按钮的弹出窗口中
- spring - Spring Boot REST - 使用 OData 从 Microsoft Dynamics NAV 中提取数据
- python - 如何在python中数值求解差分方程
- python - 用于列出基于选择日期创建的 EBS 快照的 python 脚本
- arrays - Angular 5:根据属性获取数组的所有对象
- react-native - 在 create-react-native-app 项目中突然看到错误“插件/预设文件不允许导出对象,只有功能”