我将Searchbar和SearchView组件material3(v1.1.1)添加到项目中 碎片之家 运行项目时出现错误: 错误:
android.view.InflateException:com.besoyeit.appbesoyeit:layout/fragment_home 中的二进制 XML 文件行 #52:com.besoyeit.appbesoyeit:layout/fragment_home 中的二进制 XML 文件行 #52:膨胀类 com.google.android 时出错.material.search.SearchView 引起原因:android.view.InflateException:com.besoyeit.appbesoyeit 中的二进制 XML 文件第 52 行:layout/fragment_home:膨胀类 com.google.android.material.search.SearchView 时出错 引起原因:java.lang.reflect.InitationTargetException ....
请帮助我!
摇篮 ///材料 实现(“androidx.compose.material3:material3:1.1.1”) 实现(“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">
<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.compose.material3:material3
工件适用于实现 Material3 指南的 可组合项。您使用的代码是基于视图的布局。
如果您想使用
SearchView
XML 视图,则必须使用 基于视图的库,可以在 com.google.android.material:material
工件下找到:
dependencies {
implementation("com.google.android.material:material:1.9.0")
}
截至撰写本文时,最新稳定版本为 1.9.0。
如果这是您第一次在项目中使用 Material Components for Android 库,我建议您按照 入门指南来设置库,然后您应该能够使用
SearchView
XML 视图没有任何问题。