Skip to content Skip to sidebar Skip to footer

Android Kotlin: Java.lang.illegalstateexception

This is an extension to Kotlin using Gson to deserialize local json file that I posted earlier. I want NewsFragment.kt to instantiate an adapter but am unable to access to the recy

Solution 1:

You are calling your read_json() function in your onCreate block of your fragment, at that point your view is null.

Move invocation upon read_json from onCreate to onViewCreated.

overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
   read_json(view)
}

In addition to that in the read_json function modify worldnews to view.worldnews:

activity?.runOnUiThread {
   view.worldnews.adapter = MainAdapter(homeFeed)
}

Solution 2:

The view has not been inflated yet, therefore worldnews doesn't exist when you execute read_json().

Move read_json() inside onResume.

overridefunonResume() {
        super.onResume()
        read_json()
    }

Otherwise you can only access it by specifying the inflated view first, like you do already inside your onCreate:

view.worldnews.layoutManager = LinearLayoutManager(activity)

Post a Comment for "Android Kotlin: Java.lang.illegalstateexception"