androidkotlinsearchviewmaterial3

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")


<?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.coordinatorlayout.widget.CoordinatorLayout>


Solution

  • 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.