英文:
Material3 Error inflating class com.google.android.material.search.SearchView
问题 {#heading}
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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/frame_main_constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
`</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">
<com.google.android.material.search.SearchBar
android:id="@+id/searchBar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:contentDescription="@string/app_name"
android:hint="@string/searchbar_hint"
android:layoutDirection="rtl"
android:minHeight="48dp"
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")
<?xml version="1.0" encoding="utf-8"?>
<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\"\>
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:id=&quot;@+id/frame_main_constraintLayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
\</androidx.core.widget.NestedScrollView\>
\<com.google.android.material.appbar.AppBarLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:background=\"@color/white\"\>
&lt;com.google.android.material.search.SearchBar
android:id=&quot;@+id/searchBar_main&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginTop=&quot;16dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:contentDescription=&quot;@string/app_name&quot;
android:hint=&quot;@string/searchbar_hint&quot;
android:layoutDirection=&quot;rtl&quot;
android:minHeight=&quot;48dp&quot;
android:padding=&quot;4dp&quot; /&gt;
\</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>
答案1 {#1}
得分: 1
androidx.compose.material3:material3
构件是用于实现 Material3 指南 的 可组合项。你正在使用的代码是基于 视图布局。
如果你想使用 SearchView
XML 视图,你需要使用 基于视图的库,可以在 com.google.android.material:material
构件下找到:
dependencies {
implementation("com.google.android.material:material:1.9.0")
}
截至撰写本文时,最新稳定版本为 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:
dependencies {
implementation("com.google.android.material:material:1.9.0")
}
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.