IllegalStateException:无法将 SemanticsComponentNode 转换为视图
IllegalStateException: Could not convert SemanticsComponentNode to a View
我有一个非常简单的 @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.invoke(Composer.kt:1049)
at androidx.compose.Composer$realizeSlots.invoke(Composer.kt:178)
at androidx.compose.Composer$applyChanges.invoke(Composer.kt:270)
at androidx.compose.Composer$applyChanges.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.invoke(CompositionContext.kt:104)
at androidx.compose.CompositionContext$recompose.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)
通过将 Greeting
可组合项包装在 CraneWrapper
和 RallyTheme
.
中解决了这个问题
class RallyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CraneWrapper {
RallyTheme {
Greeting("Tom")
}
}
}
}
}
我有一个非常简单的 @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.invoke(Composer.kt:1049)
at androidx.compose.Composer$realizeSlots.invoke(Composer.kt:178)
at androidx.compose.Composer$applyChanges.invoke(Composer.kt:270)
at androidx.compose.Composer$applyChanges.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.invoke(CompositionContext.kt:104)
at androidx.compose.CompositionContext$recompose.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)
通过将 Greeting
可组合项包装在 CraneWrapper
和 RallyTheme
.
class RallyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CraneWrapper {
RallyTheme {
Greeting("Tom")
}
}
}
}
}