Kotlin 如何初始化lateinit变量?
我正在尝试运行开源应用程序,在该应用程序中,我得到的错误为Kotlin 如何初始化lateinit变量?,kotlin,kotlin-lateinit,Kotlin,Kotlin Lateinit,我正在尝试运行开源应用程序,在该应用程序中,我得到的错误为 lateinit property textInput has not been initialized 使用文本输入的主代码 class MainActivity : BaseActivity() { private val drawerToggle by lazy { ActionBarDrawerToggle(this, drawer_layout, drawer_open, drawer_close) }
lateinit property textInput has not been initialized
使用文本输入的主代码
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)
})
class MainActivity:BaseActivity(){
private val drawerToggle通过lazy{ActionBarDrawerToggle(这个,drawer\u布局,drawer\u打开,drawer\u关闭)}
私有val survivalContent by lazy{survivalContent(资产)}
私有lateinit变量currentUrl:String
私有lateinit变量currentTopicName:字符串
私有lateinit变量textInput:MutableList
private var lastFontSize=State.getFontSize()
private var lastNightMode=State.nightModerString()
private var lastAllowSelect=State.allowSelect()
由lazy{linearLayoutManager(this)}生成的私有val linearLayoutManager
私有变量按可观测值isInEditMode(false,onChange={{u0,{u0,newMode->
如果(新模式){
制作setImageResource(可绘制的ic图像移除红眼)
contentRecycler.adapter=EditingRecyclerAdapter(textInput)
}否则{
工厂设置图像资源(可绘制的ic编辑器模式编辑)
contentRecycler.adapter=MarkdownRecyclerAdapter(textInput,imageWidth(),onURLClick)
}
contentRecycler.scrollToPosition(State.lastScrollPos)
})
由于我是android开发新手,我不知道如何在使用lateinit变量之前初始化它。在此之前,我曾尝试过本文中提到的
git子模块更新
,但没有成功。因此,现在我希望问题在于初始化textInput变量。您必须初始化textInpu在使用变量之前,请先输入t
。
为此,您可以执行以下操作:
textInput = mutableListOf("Abc", "Xyz")
来自文档:在初始化lateinit属性之前访问该属性会引发一个特殊异常,该异常清楚地标识正在访问的属性以及尚未初始化的事实 阅读更多信息:执行以下操作:
private var currentUrl: String =""
private var currentTopicName: String =""
private var textInput= mutableListOf<String>()
如果需要更多帮助,请告诉我。在使用
textInput
之前,您需要执行类似textInput=mutableListOf(“Abc”、“Xyz”)的操作伟大的,如果你的应用程序运行的话,如果你的应用程序没有任何问题,但是里面的内容是空的,我就不需要任何帮助了,也请任何帮助。我不知道所有的方法都是空白的。这是因为我从开源或其他任何问题中拿走了这个应用程序,这里是App.请分享我如果YO。你对这个问题一无所知。
private lateinit var currentUrl: String
private lateinit var currentTopicName: String
private lateinit var textInput: MutableList<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
currentUrl = String()
currentTopicName = String()
textInput= mutableListOf()
}