Jetpack Compose Render 问题 NullPointer Exception in Design Screen
Jetpack Compose Render Problem NullPointer Exception in Design Screen
我创建了一个 activity,当我 运行 我在我的 android 设备上看到了结果,但我无法使用 @Preview @Composable
中的注释看到结果设计屏幕,我总是看到渲染问题。有空指针异常。
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
createViews();
}
}
@Composable
fun createViews(){
ScrollableColumn(
modifier = Modifier
.fillMaxSize()
.background(color = Color(0xFFf2f2f2))
)
{
Image(
bitmap = imageFromResource(res = resources,resId = R.drawable.logo_2),
modifier = Modifier
.size(100.dp, 200.dp)
.align(alignment = Alignment.CenterHorizontally),
contentScale = ContentScale.Crop
)
Column(
Modifier.padding(20.dp)
) {
Text(
text = "Archi",
style = TextStyle(
color = Color(0xFFffd600),
fontSize = TextUnit.Companion.Sp(20)
)
)
Spacer(modifier = Modifier.padding(10.dp))
Text(
text = "test",
style = TextStyle(
color = Color(0xFFf4511e),
fontSize = TextUnit.Companion.Sp(20)
)
)
Spacer(modifier = Modifier.padding(10.dp))
Text(
text = "xxx-xxxxxxx",
style = TextStyle(
color = Color(0xFF00e676),
fontSize = TextUnit.Companion.Sp(20)
)
)
}
}
}
@Preview
@Composable
fun defaultPreview(){
//Conmposable
createViews();
}
}
异常:
java.lang.NullPointerException at
android.content.ContextWrapper.getResources(ContextWrapper.java:97)
at
android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
at
android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
at
androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:592)
at
com.example.mvvmapp.MainActivity$createViews.invoke(MainActivity.kt:43)
at
com.example.mvvmapp.MainActivity$createViews.invoke(MainActivity.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:155)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at
androidx.compose.foundation.ScrollKt.ScrollableColumn(Scroll.kt:528)
at com.example.mvvmapp.MainActivity.createViews(MainActivity.kt:34)
at
com.example.mvvmapp.MainActivity.defaultPreview(MainActivity.kt:89)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566) at
androidx.compose.ui.tooling.preview.PreviewUtilsKt.invokeComposableMethod(PreviewUtils.kt:141)
at
androidx.compose.ui.tooling.preview.PreviewUtilsKt.invokeComposableViaReflection(PreviewUtils.kt:185)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$composable.invoke(ComposeViewAdapter.kt:437)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$composable.invoke(ComposeViewAdapter.kt)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:471)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at
androidx.compose.ui.tooling.InspectableKt.Inspectable(Inspectable.kt:65)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$WrapPreview.invoke(ComposeViewAdapter.kt:384)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$WrapPreview.invoke(ComposeViewAdapter.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at
androidx.compose.ui.tooling.preview.ComposeViewAdapter.WrapPreview(ComposeViewAdapter.kt:383)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.kt)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:430)
at
androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at
androidx.compose.ui.platform.ComposeView.Content(ComposeView.kt:293)
at
androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.kt:161)
at
androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at
androidx.compose.ui.platform.AmbientsKt.ProvideCommonAmbients(Ambients.kt:276)
at
androidx.compose.ui.platform.AndroidAmbientsKt$ProvideAndroidAmbients.invoke(AndroidAmbients.kt:176)
at
androidx.compose.ui.platform.AndroidAmbientsKt$ProvideAndroidAmbients.invoke(AndroidAmbients.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at
androidx.compose.ui.platform.AndroidAmbientsKt.ProvideAndroidAmbients(AndroidAmbients.kt:168)
at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:261)
at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:260)
at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)
at
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt)
at
androidx.compose.runtime.ComposerKt.invokeComposable(Composer.kt:2906)
at androidx.compose.runtime.Composer.composeInitial(Composer.kt:2046)
at
androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:566)
at
androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:109)
at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:243)
at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt)
at
androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.kt:548)
at
androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.kt:234)
at
androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.kt:285)
at
androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at
androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)
at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:241)
at
androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt)
at
androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.kt:617)
at android.view.View.dispatchAttachedToWindow(View.java:20479) at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at
android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44)
at
com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
at
com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at
com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:696)
at
com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:852)
at
com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:187)
at
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
依赖关系
def composeVersion = "1.0.0-alpha10"
implementation "androidx.compose.ui:ui:$composeVersion"
// Tooling support (Previews, etc.)
implementation "androidx.compose.ui:ui-tooling:$composeVersion"
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation "androidx.compose.foundation:foundation:$composeVersion"
// Material Design
implementation "androidx.compose.material:material:$composeVersion"
// Material design icons
implementation "androidx.compose.material:material-icons-core:$composeVersion"
implementation "androidx.compose.material:material-icons-extended:$composeVersion"
// Integration with observables
implementation "androidx.compose.runtime:runtime-livedata:$composeVersion"
implementation "androidx.compose.runtime:runtime-rxjava2:$composeVersion"
预览适用于我使用 1.0.0-alpha11。
我必须添加这个:
val resources = AmbientContext.current.resources
以及图片的内容描述,如下所示:
val resources = AmbientContext.current.resources
Image(
contentDescription = "",
bitmap = imageFromResource(res = resources,resId = R.drawable.image),
modifier = Modifier
.size(100.dp, 200.dp)
.align(alignment = Alignment.CenterHorizontally),
contentScale = ContentScale.Crop
)
您确定使用的是 Android Studio 的金丝雀版本吗?
我创建了一个 activity,当我 运行 我在我的 android 设备上看到了结果,但我无法使用 @Preview @Composable
中的注释看到结果设计屏幕,我总是看到渲染问题。有空指针异常。
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
createViews();
}
}
@Composable
fun createViews(){
ScrollableColumn(
modifier = Modifier
.fillMaxSize()
.background(color = Color(0xFFf2f2f2))
)
{
Image(
bitmap = imageFromResource(res = resources,resId = R.drawable.logo_2),
modifier = Modifier
.size(100.dp, 200.dp)
.align(alignment = Alignment.CenterHorizontally),
contentScale = ContentScale.Crop
)
Column(
Modifier.padding(20.dp)
) {
Text(
text = "Archi",
style = TextStyle(
color = Color(0xFFffd600),
fontSize = TextUnit.Companion.Sp(20)
)
)
Spacer(modifier = Modifier.padding(10.dp))
Text(
text = "test",
style = TextStyle(
color = Color(0xFFf4511e),
fontSize = TextUnit.Companion.Sp(20)
)
)
Spacer(modifier = Modifier.padding(10.dp))
Text(
text = "xxx-xxxxxxx",
style = TextStyle(
color = Color(0xFF00e676),
fontSize = TextUnit.Companion.Sp(20)
)
)
}
}
}
@Preview
@Composable
fun defaultPreview(){
//Conmposable
createViews();
}
}
异常:
java.lang.NullPointerException at android.content.ContextWrapper.getResources(ContextWrapper.java:97) at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128) at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:592) at com.example.mvvmapp.MainActivity$createViews.invoke(MainActivity.kt:43) at com.example.mvvmapp.MainActivity$createViews.invoke(MainActivity.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:155) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.foundation.ScrollKt.ScrollableColumn(Scroll.kt:528) at com.example.mvvmapp.MainActivity.createViews(MainActivity.kt:34) at com.example.mvvmapp.MainActivity.defaultPreview(MainActivity.kt:89) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at androidx.compose.ui.tooling.preview.PreviewUtilsKt.invokeComposableMethod(PreviewUtils.kt:141) at androidx.compose.ui.tooling.preview.PreviewUtilsKt.invokeComposableViaReflection(PreviewUtils.kt:185) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$composable.invoke(ComposeViewAdapter.kt:437) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$composable.invoke(ComposeViewAdapter.kt) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:471) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at androidx.compose.ui.tooling.InspectableKt.Inspectable(Inspectable.kt:65) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$WrapPreview.invoke(ComposeViewAdapter.kt:384) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$WrapPreview.invoke(ComposeViewAdapter.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at androidx.compose.ui.tooling.preview.ComposeViewAdapter.WrapPreview(ComposeViewAdapter.kt:383) at androidx.compose.ui.tooling.preview.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.kt) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:430) at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.ui.platform.ComposeView.Content(ComposeView.kt:293) at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.kt:161) at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at androidx.compose.ui.platform.AmbientsKt.ProvideCommonAmbients(Ambients.kt:276) at androidx.compose.ui.platform.AndroidAmbientsKt$ProvideAndroidAmbients.invoke(AndroidAmbients.kt:176) at androidx.compose.ui.platform.AndroidAmbientsKt$ProvideAndroidAmbients.invoke(AndroidAmbients.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at androidx.compose.ui.platform.AndroidAmbientsKt.ProvideAndroidAmbients(AndroidAmbients.kt:168) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:261) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:260) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146) at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt) at androidx.compose.runtime.ComposerKt.invokeComposable(Composer.kt:2906) at androidx.compose.runtime.Composer.composeInitial(Composer.kt:2046) at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:566) at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:109) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:243) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt) at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.kt:548) at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.kt:234) at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.kt:285) at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:241) at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt) at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.kt:617) at android.view.View.dispatchAttachedToWindow(View.java:20479) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496) at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:696) at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:852) at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:187) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834)
依赖关系
def composeVersion = "1.0.0-alpha10"
implementation "androidx.compose.ui:ui:$composeVersion"
// Tooling support (Previews, etc.)
implementation "androidx.compose.ui:ui-tooling:$composeVersion"
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation "androidx.compose.foundation:foundation:$composeVersion"
// Material Design
implementation "androidx.compose.material:material:$composeVersion"
// Material design icons
implementation "androidx.compose.material:material-icons-core:$composeVersion"
implementation "androidx.compose.material:material-icons-extended:$composeVersion"
// Integration with observables
implementation "androidx.compose.runtime:runtime-livedata:$composeVersion"
implementation "androidx.compose.runtime:runtime-rxjava2:$composeVersion"
预览适用于我使用 1.0.0-alpha11。
我必须添加这个:
val resources = AmbientContext.current.resources
以及图片的内容描述,如下所示:
val resources = AmbientContext.current.resources
Image(
contentDescription = "",
bitmap = imageFromResource(res = resources,resId = R.drawable.image),
modifier = Modifier
.size(100.dp, 200.dp)
.align(alignment = Alignment.CenterHorizontally),
contentScale = ContentScale.Crop
)
您确定使用的是 Android Studio 的金丝雀版本吗?