Material3 错误膨胀类 com.google.android.material.search.SearchView

huangapple go评论194阅读模式
英文:

Material3 Error inflating class com.google.android.material.search.SearchView

问题

I add Searchbar and SearchView component material3(v1.1.1) to project
fragment home
when run project Error:
Error :

android.view.InflateException: Binary XML file line #52 in com.besoyeit.appbesoyeit:layout/fragment_home: Binary XML file line #52 in com.besoyeit.appbesoyeit:layout/fragment_home: Error inflating class com.google.android.material.search.SearchView
Caused by: android.view.InflateException: Binary XML file line #52 in com.besoyeit.appbesoyeit:layout/fragment_home: Error inflating class com.google.android.material.search.SearchView
Caused by: java.lang.reflect.InvocationTargetException
....

please help me!

Gradle
///material
implementation("androidx.compose.material3:material3:1.1.1")
implementation("androidx.compose.material3:material3-window-size-class:1.1.1")


<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentHome">

<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

  1. <androidx.constraintlayout.widget.ConstraintLayout
  2. android:id="@+id/frame_main_constraintLayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">

  1. <com.google.android.material.search.SearchBar
  2. android:id="@+id/searchBar_main"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginStart="16dp"
  6. android:layout_marginTop="16dp"
  7. android:layout_marginEnd="16dp"
  8. android:contentDescription="@string/app_name"
  9. android:hint="@string/searchbar_hint"
  10. android:layoutDirection="rtl"
  11. android:minHeight="48dp"
  12. android:padding="4dp" />

</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.search.SearchView
android:textColor="?attr/colorAccent"
android:id="@+id/searchView_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/searchbar_hint"
app:layout_anchor="@id/searchBar_main">

</com.google.android.material.search.SearchView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

英文:

I add Searchbar and SearchView component material3(v1.1.1) to project
fragment home
when run project Error :
Error :

android.view.InflateException: Binary XML file line #52 in com.besoyeit.appbesoyeit:layout/fragment_home: Binary XML file line #52 in com.besoyeit.appbesoyeit:layout/fragment_home: Error inflating class com.google.android.material.search.SearchView
Caused by: android.view.InflateException: Binary XML file line #52 in com.besoyeit.appbesoyeit:layout/fragment_home: Error inflating class com.google.android.material.search.SearchView
Caused by: java.lang.reflect.InvocationTargetException
....

please help me!


Gradle
///material
implementation("androidx.compose.material3:material3:1.1.1")
implementation("androidx.compose.material3:material3-window-size-class:1.1.1")


  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.FragmentHome&quot;&gt;
  8. &lt;androidx.core.widget.NestedScrollView
  9. android:id=&quot;@+id/nestedScrollView&quot;
  10. android:layout_width=&quot;match_parent&quot;
  11. android:layout_height=&quot;match_parent&quot;
  12. android:contentDescription=&quot;@string/app_name&quot;
  13. app:layout_behavior=&quot;com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior&quot;&gt;
  14. &lt;androidx.constraintlayout.widget.ConstraintLayout
  15. android:id=&quot;@+id/frame_main_constraintLayout&quot;
  16. android:layout_width=&quot;match_parent&quot;
  17. android:layout_height=&quot;match_parent&quot;&gt;
  18. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  19. &lt;/androidx.core.widget.NestedScrollView&gt;
  20. &lt;com.google.android.material.appbar.AppBarLayout
  21. android:layout_width=&quot;match_parent&quot;
  22. android:layout_height=&quot;wrap_content&quot;
  23. android:background=&quot;@color/white&quot;&gt;
  24. &lt;com.google.android.material.search.SearchBar
  25. android:id=&quot;@+id/searchBar_main&quot;
  26. android:layout_width=&quot;match_parent&quot;
  27. android:layout_height=&quot;wrap_content&quot;
  28. android:layout_marginStart=&quot;16dp&quot;
  29. android:layout_marginTop=&quot;16dp&quot;
  30. android:layout_marginEnd=&quot;16dp&quot;
  31. android:contentDescription=&quot;@string/app_name&quot;
  32. android:hint=&quot;@string/searchbar_hint&quot;
  33. android:layoutDirection=&quot;rtl&quot;
  34. android:minHeight=&quot;48dp&quot;
  35. android:padding=&quot;4dp&quot; /&gt;
  36. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  37. &lt;com.google.android.material.search.SearchView
  38. android:textColor=&quot;?attr/colorAccent&quot;
  39. android:id=&quot;@+id/searchView_main&quot;
  40. android:layout_width=&quot;match_parent&quot;
  41. android:layout_height=&quot;match_parent&quot;
  42. android:hint=&quot;@string/searchbar_hint&quot;
  43. app:layout_anchor=&quot;@id/searchBar_main&quot;&gt;
  44. &lt;/com.google.android.material.search.SearchView&gt;

</androidx.coordinatorlayout.widget.CoordinatorLayout>

答案1

得分: 1

androidx.compose.material3:material3 构件是用于实现 Material3 指南可组合项。你正在使用的代码是基于 视图布局

如果你想使用 SearchView XML 视图,你需要使用 基于视图的库,可以在 com.google.android.material:material 构件下找到:

  1. dependencies {
  2. implementation("com.google.android.material:material:1.9.0")
  3. }

截至撰写本文时,最新稳定版本为 1.9.0

如果这是你第一次在项目中使用 Android 的 Material 组件库,我建议你按照入门指南设置库,然后你应该可以无问题地使用 SearchView XML 视图

英文:

The androidx.compose.material3:material3 artifact is for composables that implement the Material3 guidelines. The code you're using is a views-based layout.

If you want to use the SearchView XML view, you'll have to use the views-based library which can be found under the com.google.android.material:material artifact:

  1. dependencies {
  2. implementation(&quot;com.google.android.material:material:1.9.0&quot;)
  3. }

As of the time of writing, the latest stable version is 1.9.0.

If this is your first time using the Material Components for Android library in your project, I would recommend you follow the getting started guide to setup the library, and then you should be able to use the SearchView XML view with no issues.

huangapple
  • 本文由 发表于 2023年8月11日 01:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定