我正在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
您需要使用
<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>