无法解析类 SearchView。

5

我正在尝试在我的应用程序中添加搜索栏,但遇到了“无法解析类SearchView”的错误。由于谷歌没有提供帮助,因此我来到这里。这是我的代码:

<SearchView
        android:layout_width="410dp"
        android:layout_height="44dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="2dp"
        app:layout_constraintBottom_toTopOf="@+id/grid_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

其余的xml都是正确的,我直接从Android获取了小部件,所以这让我感到困惑。非常感谢所有的帮助。


我已解决了这个问题,但仍然欢迎其他人输入以造福他人。 - The_Programming_Taco_Noob
如果您已经解决了问题,请回答自己的问题。 - mightyandweakcoder
2个回答

12

也许您需要指定完整的搜索视图路径以获取Android组件。

<android.widget.SearchView
    android:layout_width="410dp"
    android:layout_height="44dp"
    android:layout_marginEnd="1dp"
    android:layout_marginBottom="2dp"
    app:layout_constraintBottom_toTopOf="@+id/grid_view"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

5

鉴于提问者没有回答解决方案,现在将其作为答案发布。

如果您的项目正在使用 androidx,则需要在布局中使用 androidx 组件(当在设计面板中放置 SearchView 项时,这尚不是默认选项)。

<androidx.appcompat.widget.SearchView
        android:layout_width="410dp"
        android:layout_height="44dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="2dp"
        app:layout_constraintBottom_toTopOf="@+id/grid_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接