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

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

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

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


&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout          xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.FragmentHome&quot;&gt;

&lt;androidx.core.widget.NestedScrollView
    android:id=&quot;@+id/nestedScrollView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:contentDescription=&quot;@string/app_name&quot;
    app:layout_behavior=&quot;com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior&quot;&gt;

    &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;

&lt;/androidx.core.widget.NestedScrollView&gt;

&lt;com.google.android.material.appbar.AppBarLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:background=&quot;@color/white&quot;&gt;

    &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;

&lt;/com.google.android.material.appbar.AppBarLayout&gt;

&lt;com.google.android.material.search.SearchView
    android:textColor=&quot;?attr/colorAccent&quot;
    android:id=&quot;@+id/searchView_main&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:hint=&quot;@string/searchbar_hint&quot;
    app:layout_anchor=&quot;@id/searchBar_main&quot;&gt;
   
&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 构件下找到:

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(&quot;com.google.android.material:material:1.9.0&quot;)
}

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:

确定