1

我有一个非常简单的@Composable功能,我想在我的布局中使用它。但是,它最终抛出了一个IllegalStateException,我无法弄清楚我在这里缺少什么。

class RallyActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      Greeting("Tom")
    }
  }
}

@Composable
fun Greeting(name: String) {
  Text(
      "Hello, $name",
      TextStyle(fontSize = 48.0F)
  )
}
2019-05-09 06:32:33.214 5493-5493/androidx.ui.material.studies E/AndroidRuntime: FATAL EXCEPTION: main
    Process: androidx.ui.material.studies, PID: 5493
    java.lang.RuntimeException: Unable to start activity ComponentInfo{androidx.ui.material.studies/androidx.ui.material.studies.rally.RallyActivity}: java.lang.IllegalStateException: Could not convert SemanticsComponentNode to a View
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalStateException: Could not convert SemanticsComponentNode to a View
        at androidx.compose.ViewApplyAdapter.end(ViewComposer.kt:104)
        at androidx.compose.Applier.up(Applier.kt:53)
        at androidx.compose.Composer$realizeSlots$10.invoke(Composer.kt:1049)
        at androidx.compose.Composer$realizeSlots$10.invoke(Composer.kt:178)
        at androidx.compose.Composer$applyChanges$1.invoke(Composer.kt:270)
        at androidx.compose.Composer$applyChanges$1.invoke(Composer.kt:178)
        at androidx.compose.SlotTable.write(SlotTable.kt:689)
        at androidx.compose.Composer.applyChanges(Composer.kt:269)
        at androidx.compose.CompositionContext$recompose$1.invoke(CompositionContext.kt:104)
        at androidx.compose.CompositionContext$recompose$1.invoke(CompositionContext.kt:24)
        at androidx.compose.CompositionContextKt.runWithCurrent(CompositionContext.kt:128)
        at androidx.compose.CompositionContext.recompose(CompositionContext.kt:94)
        at androidx.compose.Compose.composeInto(Compose.kt:135)
        at androidx.compose.ComposeKt.compose(Compose.kt:265)
        at androidx.compose.ComposeKt.setContent(Compose.kt:239)
        at androidx.ui.material.studies.rally.RallyActivity.onCreate(RallyActivity.kt:46)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

4

1 回答 1

2

Greeting通过将可组合组件包装在 aCraneWrapper和 a中解决了这个问题RallyTheme

class RallyActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      CraneWrapper {
        RallyTheme {
          Greeting("Tom")
        }
      }
    }
  }
}
于 2019-05-13T03:26:54.617 回答