尝试渲染此预览时发现一些问题-Android Studio Dolphin

c9qzyr3d  于 2022-09-21  发布在  Android
关注(0)|答案(1)|浏览(581)

今天,我把我的Android Studio从花栗鼠升级到了海豚,我的大多数Composable预览版都停止工作了,出现错误Some issues were found when trying to render this preview

在单击问题面板中的显示异常按钮时,我发现以下错误。

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.

我的大多数可组合函数使用Firebase Analytics进行事件日志记录,因此FirebaseAnalytics是几乎所有可组合函数的参数之一。在Android Studio Chipmunk之前,我可以按照下面的方法显示所有可组合组件的预览。

  1. @Preview
  2. @Composable
  3. private fun DemoPreview() {
  4. MyAppTheme {
  5. Demo(firebaseAnalytics = Firebase.analytics, onClick = {})
  6. }
  7. }

但上述方法不再适用于Android Studio Dolphin。

有没有其他方法可以传递FirebaseAnalytics对象,使Composable Pview再次工作?

oyxsuwqo

oyxsuwqo1#

当您的Composable有参数时,我建议将实际功能和UI完全分开:

  1. @Composable
  2. private fun DemoContent(analyticsCallback: (input: String) -> Unit, onClick: () -> Unit) {
  3. // Your UI - call analyticsCallback when needed
  4. }
  5. @Composable
  6. fun DemoScreen(firebaseAnalytics: FirebaseAnalytics, onClick: () -> Unit) {
  7. DenoContent(
  8. analyticsCallback = { input ->
  9. // Process input with firebase
  10. },
  11. onClick
  12. )
  13. }
  14. @Preview
  15. @Composable
  16. private fun DemoPreview() {
  17. MyAppTheme {
  18. DemoContent(
  19. analyticsCallback = { input ->
  20. // Log the input
  21. },
  22. onClick = { }
  23. )
  24. }
  25. }

input只是一个占位符-将其更改为使用Firebase所需的任何内容。

展开查看全部

相关问题