https://kotlinlang.org logo
#compose
Title
# compose
a

amar_1995

11/11/2019, 6:53 AM
I am trying to view a list of item. From mainActivity class, I am passing arrayList of item to composable function. But while running the app, I am getting this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{<http://package.name/package.name.MainActivity|package.name/package.name.MainActivity>}: java.lang.IllegalStateException: Composition requires an active composition context
My mainActivity class
Copy code
class MainActivity : AppCompatActivity() {
    var dashboardList: ArrayList<Dashboard> = ArrayList()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        fillDummyData()
        setContent {
           MainScreen(dashboardList)
        }
    }
    fun fillDummyData() {
        dashboardList.add(
            Dashboard(title="Sample title",preview="nfjsad dsaf  safsadf ", image= +imageResource(R.drawable.icon), dateTime = "Aug 10", isOnline = "true")
        )
    }
}
If i remove fillDummyData function everything is working fine.
l

Luca Nicoletti

11/11/2019, 8:49 AM
You’re calling
Dashboard
which I guess is a
@composable
function outside a composition
Try adding a
model
to the list, and reference the model from a
@Composable
function
a

amar_1995

11/11/2019, 8:51 AM
Dashboard is a data class
Okay I got your point +imageResource is composable function Now making
fillDummyData
function
@Composable
is working
l

Luca Nicoletti

11/11/2019, 9:02 AM
Hmmmm
Not sure that’s the best solution though
I’d store the ID in the dataclass
and retrieve the actual image inside a
composition
so from a function which is somewhere `inside`/`nested` in the
setContent
- which is to be considered a starting point for composition
as it interally calls
Compose.composeInto()
a

amar_1995

11/11/2019, 9:03 AM
Okay i will do the same Thanks.
l

Luca Nicoletti

11/11/2019, 9:03 AM
Nw .:)