Search code examples
androidandroid-auto

IllegalStateException:Template is in a loading state but lists are added, or vice versa


I want to use EventBus to set new state and rerender Android Auto Screen.

package com.capsule

import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class CarPlayScreen(carContext: CarContext) : Screen(carContext) {

    private var template: Template
    private var eventBus = EventBus.getDefault()

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onCapsuleStateUpdate(state: CapsuleState) {
        template = GridTemplate.Builder()
            .setTitle("Temperature: ${state.temperature}C")
            .build()
    }


    init {
        eventBus.register(this)

        val row = Row.Builder()
            .setTitle("Wait state update")
            .build()
        val pane = Pane.Builder()
            .addRow(row)
            .build()

        template = PaneTemplate.Builder(pane)
            .setHeaderAction(Action.APP_ICON)
            .build()

        invalidate()
    }

    override fun onGetTemplate(): Template {
        return template
    }

}

When I run app on emulator I see init template

enter image description here

But after calling onCapsuleStateUpdate rerender doesn't work and in console I see:

E  Could not dispatch event: class com.capsule.CapsuleState to subscribing class class com.capsule.CarPlayScreen
java.lang.IllegalStateException: Template is in a loading state but lists are added, or vice versa
at androidx.car.app.model.GridTemplate$Builder.build(GridTemplate.java:282)
at com.capsule.CarPlayScreen.onCapsuleStateUpdate(CarPlayScreen.kt:24)
at java.lang.reflect.Method.invoke(Native Method)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:517)
at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:444)
at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:421)
at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:394)
at org.greenrobot.eventbus.EventBus.post(EventBus.java:275)
at com.capsule.CapsuleBleManager.followState$lambda$10(CapsuleBleManager.kt:190)
at com.capsule.CapsuleBleManager.$r8$lambda$6x0OdCCBxFndfQ6AFD4N11VDXro(Unknown Source:0)
at com.capsule.CapsuleBleManager$$ExternalSyntheticLambda0.onDataReceived(Unknown Source:2)
at no.nordicsemi.android.ble.ValueChangedCallback.lambda$notifyValueChanged$0(ValueChangedCallback.java:184)
at no.nordicsemi.android.ble.ValueChangedCallback$$ExternalSyntheticLambda1.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:914)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Description of the error

IllegalStateException – if the template is in a loading state but there are lists added, or vice versa, or if the template does not have either a title or header Action set.

But I can't understand why template is in loading state if it was rendered. How to fix it?


Solution

  • The issue is that your GridTemplate is not marked as loading but also has no content to display.

    I'm not really sure why you're using a GridTemplate if there's no content to display in it, but you can fix this issue by either marking the GridTemplate as loading by calling setLoading(true) on the builder or by calling setSingleList(...) with an ItemList.