【问题标题】:Error inflating class android.support.v7.widget.SearchView - android错误膨胀类 android.support.v7.widget.SearchView - android
【发布时间】:2018-11-02 07:27:10
【问题描述】:

我在我的 Fragment 类中实现了一个 searchView。但是有这个错误。当我也尝试调试时,应用程序因此错误而崩溃。

在stackoverflow中看到了一些类似的问题,但都没有解决问题,所以我创建了一个新问题

这是我的代码:

<android.support.constraint.ConstraintLayout 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="wrap_content"
android:focusable="true"
android:background="@color/colorBackground"
android:focusableInTouchMode="true"
tools:context="com.work.ksd.HomePage">

<android.support.v7.widget.SearchView
    android:id="@+id/searchView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="0dp"
    app:queryHint="Search products"
    app:iconifiedByDefault="false"
    android:elevation="5dp"
    android:focusable="false"
    android:forceHasOverlappingRendering="false"
    android:focusableInTouchMode="false"
    app:theme="@style/AppSearchView"
    app:queryBackground="@android:color/transparent"
    android:background="@drawable/register_textview_background"
    android:layout_marginRight="10dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="10dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"/>

</android.support.constraint.ConstraintLayout>

关于我的片段的 CreateView 方法:

SearchView searchView = (SearchView) view.findViewById(R.id.searchView);

错误是

    android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.v7.widget.SearchView

【问题讨论】:

  • 您是否导入了正确的 SearchView 类?也许是android.widget.SearchView 而不是android.support.v7.widget.SearchView
  • 不,我正在导入这个 --> import android.support.v7.widget.SearchView;
  • 尝试删除这些并检查它是否有效.. app:queryBackground="@android:color/transparent" android:background="@drawable/register_textview_background" 可能是你的drawable有问题
  • @SantanuSur 不,还是一样
  • @SantanuSur 是的,你是对的,在 styles.xml 中,有一个额外的 导致了错误,我的愚蠢错误。谢谢!

标签: android searchview


【解决方案1】:

你需要使用

<androidx.appcompat.widget.SearchView

代替

<android.support.v7.widget.SearchView

您的 xml 代码:

<android.support.constraint.ConstraintLayout 
   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="wrap_content"
   android:focusable="true"
   android:background="@color/colorBackground"
   android:focusableInTouchMode="true"
   tools:context="com.work.ksd.HomePage">

<androidx.appcompat.widget.SearchView
    android:id="@+id/searchView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:padding="0dp"
    app:queryHint="Search products"
    app:iconifiedByDefault="false"
    android:elevation="5dp"
    android:focusable="false"
    android:forceHasOverlappingRendering="false"
    android:focusableInTouchMode="false"
    app:theme="@style/AppSearchView"
    app:queryBackground="@android:color/transparent"
    android:background="@drawable/register_textview_background"
    android:layout_marginRight="10dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="10dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"/>

</android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2014-10-18
    • 2015-01-06
    • 2014-12-21
    相关资源
    最近更新 更多