16

I have an BaseUi class for my custom views and my activities extends from it.
whe i using Root function , application crashed.

BaseUi.kt

@ExperimentalAnimationApi
open class BaseUi : AppCompatActivity() {

@Composable
fun RtlView(content: @Composable () -> Unit) {
    CompositionLocalProvider(
        LocalLayoutDirection provides LayoutDirection.Rtl,
        content = content
    )
}

@Composable
fun LtrView(content: @Composable () -> Unit) {
    CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
        content()
    }
}

@Composable
fun Root(
    content: @Composable () -> Unit
) {
    KasbTheme {
        Box(
            Modifier
                .fillMaxSize()
                .background(LightPageBackground)
                .padding(Dimen.pagePadding)
        ){
            content()
        }
    }
}
}

SplashActivity.kt

@ExperimentalAnimationApi
class SplashActivity : BaseActivity() {

val viewModel = SplashActivityViewModel()

@ExperimentalAnimationApi
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        InitUI()
    }
}

@ExperimentalAnimationApi
@Preview(showBackground = true)
@Composable
fun InitUI() {
    Root {
        RtlView {
            Box(Modifier.fillMaxSize()) {
                Image(
                    painter = painterResource(id = R.drawable.logo),
                    contentDescription = "",
                    modifier = Modifier.size(200.dp).align(Alignment.Center)
                )
            }
        }
    }
}
}

Runtime error

com.kasb.android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kasb.android, PID: 22387
    java.lang.ClassCastException: androidx.compose.runtime.internal.ComposableLambdaImpl cannot be cast to kotlin.jvm.functions.Function0
        at com.kasb.android.ui.activity.SplashActivity.InitUI(SplashActivity.kt:76)
        at com.kasb.android.ui.activity.SplashActivity$onCreate$1.invoke(SplashActivity.kt:30)
        at com.kasb.android.ui.activity.SplashActivity$onCreate$1.invoke(SplashActivity.kt:29)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:384)
1
  • 1
    Were you able to fix it? Commented Sep 30, 2021 at 13:04

3 Answers 3

5

not a permanent fix, but clean project gets rid of the error for one compile. Don't know why it's happening though.

Sign up to request clarification or add additional context in comments.

2 Comments

did you find a workarround to avoid the cleaning of the project?
I don't think so, we moved on to a new project so I don't remember, but it doesn't happen in our new project at all. So maybe update all libraries and the IDE.
2

I had same error, and problem was caused that I was calling function form subtype of the class where it was defined

    BaseActivity{
     fun displayAlertDialog(content: @Composable() () -> Unit) {
        bottomDialog.show(supportFragmentManager, "bottom-dialog")
        bottomDialog.setContent(content)
    }
            @Composable
            fun Screen(
                activity: BaseActivity,
            ) {
            activity.displayAlertDialog{} 
            }
        
        // works fine
        //if activity is subtype of BaseActivity it throws that error
    

2 Comments

Does this mean comose doesn't work well with inheritance? I've been experiencing this and don't know how to solve it if the inheritance is required.
I have the same issue where inheritance is causing a problem. But in my case, it was two levels up. Basically, a Grandfather class (GrandFather -> Father -> Child) had a function that I could not access in the child class. If the function was in the Father class, it worked fine. Extension functions work well, though, in this case, so I move my functions to be extension functions with composable blocks as parameters.
0

simple way, you just put the composable function into Class delegate,

writer.writeLine(${binding}.${handleMethod}(${IssueFixClass::class.simpleName}.${IssueFixClass.Delegate::makeClass.name}(contentListBlock$timestamp)))

result like this

binding.handleUIGroupCreator( IssueFixClass.makeClass(content64966723696806))

my kmm androidMain

actual class IssueFixClass private actual constructor(val content:@Composable @UiComposable ()->Unit){
    @Composable
    @UiComposable
    actual fun invoke(){
        content.invoke()
    }
    actual companion object Delegate{
        actual fun makeClass(content: @Composable @UiComposable () -> Unit):IssueFixClass{
          return  IssueFixClass(content)
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.