Android 无法在init块中修改kotlin lazy initialized属性

Android 无法在init块中修改kotlin lazy initialized属性,android,kotlin,constructor,Android,Kotlin,Constructor,我的类中有一些属性是惰性初始化的。我希望它们是不可变的,这就是为什么我不使用lateinit,也不希望它们是可空的,所以我认为lazy是最好的选择 在类的init块中,我想修改其中一个属性,但它给了我编译错误:变量“mLstQuestions”必须初始化 我知道惰性属性一经使用就会初始化,所以为什么会发生这种情况呢?我怎样才能解决这个问题?更好的方法是什么 如果我创建一个函数Initialize(),并在该函数中修改它。这很好,我可以在init块中调用它。为什么?这行吗?有什么区别?如果在ini

我的类中有一些属性是惰性初始化的。我希望它们是不可变的,这就是为什么我不使用
lateinit
,也不希望它们是可空的,所以我认为lazy是最好的选择

在类的
init
块中,我想修改其中一个属性,但它给了我编译错误:
变量“mLstQuestions”必须初始化

我知道惰性属性一经使用就会初始化,所以为什么会发生这种情况呢?我怎样才能解决这个问题?更好的方法是什么

如果我创建一个函数
Initialize()
,并在该函数中修改它。这很好,我可以在init块中调用它。为什么?这行吗?有什么区别?如果在
init
块中禁止这样做,那么函数调用不也应该被禁止吗

这是我的代码:

class CharacterListView(
    inflater: LayoutInflater,
    parent: ViewGroup
) {
    init {
        mLstQuestions.adapter = mQuestionsListAdapter
        // error : Variable 'mLstQuestions' must be initialized
        // error : Variable 'mQuestionsListAdapter' must be initialized
    }
    
    private val mLstQuestions by lazy { findViewById<RecyclerView>(R.id.char_list) }
    private val mQuestionsListAdapter by lazy { QuestionsListAdapter(getContext(), this) }
    private val mRootView by lazy { inflater.inflate(R.layout.activity_main, parent, false) }
...
}
类CharacterListView(
充气机,
父对象:视图组
) {
初始化{
mLstQuestions.adapter=mQuestionsListAdapter
//错误:必须初始化变量“mLstQuestions”
//错误:必须初始化变量“mQuestionsListAdapter”
}
通过lazy{findViewById(R.id.char_list)}的private val mLstQuestions
惰性{QuestionsListAdapter(getContext(),this)}的私有val mQuestionsListAdapter
lazy{inflater.inflate(R.layout.activity_main,parent,false)}生成的私有val mRootView
...
}
这是带有初始化功能的代码:

class CharacterListView(
    inflater: LayoutInflater,
    parent: ViewGroup
) {
    init { initialize() } // no errors!

    private fun initialize() {
        mLstQuestions?.adapter = mQuestionsListAdapter
    }

    private val mLstQuestions by lazy { findViewById<RecyclerView>(R.id.char_list) }
    private val mQuestionsListAdapter by lazy { QuestionsListAdapter(getContext(), this) }
    private val mRootView by lazy { inflater.inflate(R.layout.activity_main, parent, false) }
...
}
类CharacterListView(
充气机,
父对象:视图组
) {
init{initialize()}//没有错误!
私人娱乐初始化(){
mLstQuestions?.adapter=mQuestionsListAdapter
}
通过lazy{findViewById(R.id.char_list)}的private val mLstQuestions
惰性{QuestionsListAdapter(getContext(),this)}的私有val mQuestionsListAdapter
lazy{inflater.inflate(R.layout.activity_main,parent,false)}生成的私有val mRootView
...
}

请尝试在变量的
声明之后定义
init{}

class CharacterListView(
    inflater: LayoutInflater,
    parent: ViewGroup
) { 
    private val mLstQuestions by lazy { findViewById<RecyclerView>(R.id.char_list) }
    private val mQuestionsListAdapter by lazy { QuestionsListAdapter(getContext(), this) }
    private val mRootView by lazy { inflater.inflate(R.layout.activity_main, parent, false) }

init {
        mLstQuestions.adapter = mQuestionsListAdapter
    }
}
类CharacterListView(
充气机,
父对象:视图组
) { 
通过lazy{findViewById(R.id.char_list)}的private val mLstQuestions
惰性{QuestionsListAdapter(getContext(),this)}的私有val mQuestionsListAdapter
lazy{inflater.inflate(R.layout.activity_main,parent,false)}生成的私有val mRootView
初始化{
mLstQuestions.adapter=mQuestionsListAdapter
}
}

顺便说一句,当您在init块中使用惰性变量时,不需要定义它们,这是多余的。是的,您是对的,谢谢