android.widget.SearchView无法转换为android.support.v7.widget.SearchView [英] android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView

查看:113
本文介绍了android.widget.SearchView无法转换为android.support.v7.widget.SearchView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么会出现此错误.这是有问题的菜单:

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

在新的搜索活动中,我还有一个意图处理程序.为什么会显示此错误?我的最低SDK为11.

I also have an intent handler in the new search activity. Why is this error showing up? My min sdk is 11.

编辑

在onCreateOptionsMenu中:

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

推荐答案

您应该使用这些导入而不是使用支持库导入

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;

请记住,最低SDK标记为14

Just to keep in mind that the minimum SDK is marked as 14

这篇关于android.widget.SearchView无法转换为android.support.v7.widget.SearchView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆