android.widget.SearchView 无法转换为 android.support.v7.widget.SearchView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24522696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView
提问by settheline
I'm not unsure of why I'm getting this error. Here's the menu in question:
我不确定为什么会出现此错误。这是有问题的菜单:
<menu 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"
tools:context="com.example.myapp.MainActivity" >
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
Here's the searchable configuration as per the developer guide.
这是开发人员指南中的可搜索配置。
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" >
</searchable>
Added to my manifest file:
添加到我的清单文件中:
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
I also have an intent handler in the new search activity. Why is this error showing up? My min sdk is 11.
我在新的搜索活动中也有一个意图处理程序。为什么会出现这个错误?我的最小 SDK 是 11。
EDIT
编辑
In onCreateOptionsMenu:
在 onCreateOptionsMenu 中:
// Associate searchable config with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
回答by DejaVu
In your menu.xml your has to be
在你的 menu.xml 你必须是
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.support.v7.widget.SearchView" />
with that change in the last line
随着最后一行的变化
回答by Anuj
you should use these imports instead of using the support library imports
您应该使用这些导入而不是使用支持库导入
import android.app.SearchManager;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
Just to keep in mind that the minimum SDK is marked as 14
请记住,最低 SDK 标记为 14
回答by Rahel
Find import android.widget.SearchView;
in your imports and replace it with import android.support.v7.widget.SearchView
import android.widget.SearchView;
在您的导入中查找并将其替换为import android.support.v7.widget.SearchView
回答by NaturallyAsh
instead of using androids
而不是使用机器人
android:actionViewClass="android.widget.SearchView"
you have to use the apps
你必须使用这些应用程序
app:actionViewClass="android.widget.SearchView"
as per the documentation.
根据文档。
回答by user3223097
Try adding this:
尝试添加这个:
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
It worked for me.
它对我有用。
回答by Wanfadger
According to the documentation, In the onCreateOptionsMenuuse
根据文档,在onCreateOptionsMenu 中使用
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
ie:use the menu item directly to call getActionView()
method
即:直接使用菜单项调用getActionView()
方法
instead of
代替
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = MenuItemCompat.getActionView(menuItem);
or
或者
searchView = (SearchView) menu.findItem(R.id.action_search);
becouse both are deprecated
因为两者都已弃用
回答by Raheel Khan
Add this to your build.gradle file
将此添加到您的 build.gradle 文件中
implementation 'com.android.support:appcompat-v7:21.0.3'
and then add this
然后添加这个
android.support.v7.widget.SearchView
instead of
代替
// remove or replace this line
import android.widget.SearchView;
in your activity file
在您的活动文件中
回答by Mahboob Ahmed Toor
you can fixed this issue by doing this job in your code
您可以通过在代码中执行此工作来解决此问题
adding this
添加这个
import android.widget.SearchView;
导入 android.widget.SearchView;