Home > OS >  lateinit property groupList for expandableListView has not been initialized
lateinit property groupList for expandableListView has not been initialized

Time:11-18

I created a Listadapter for an expandable ListView. This adapter needs a List and a HashMap. I tried to initalize those two in a fragment with lateinit, to display this expandable ListView with custom groups and childs. But when i launch the app they have not been initalized.

The error:

 lateinit property groupList has not been initialized
class MyClass: Fragment() {

rivate lateinit var ListAdapter: YearListViewAdapter
private lateinit var groupList: List\<String\>
private lateinit var childList: HashMap\<String, List\<String\>\>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

    yearListAdapter = ListViewAdapter(this, groupList, childList) //the error occures in this line
    yearGoalList.setAdapter(ListAdapter)
    
    }
}

I dont redeclare the group and childList. I also tried to use the Listadapter later in the code but the proplem still occures.

CodePudding user response:

As the error message indicates, they are not initialized and you're passing uninitialized values. If at the beginning the list is empty and has no value I suggest doing like this:

    private val groupList: List<String> = emptyList()
    private val childList = hashMapOf<String, List<String>>()
  • Related