IllegalStateException:无法将SemanticsComponentNode转换为视图

时间:2019-05-09 01:12:32

标签: android android-jetpack-compose

我有一个非常简单的@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) 

1 个答案:

答案 0 :(得分:1)

通过将Greeting组合物包装在CraneWrapperRallyTheme中来解决此问题。

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