searchManager.getSearchableInfo(getComponentName())返回null [英] searchManager.getSearchableInfo(getComponentName()) returns null

查看:307
本文介绍了searchManager.getSearchableInfo(getComponentName())返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已遵循本Android指南以添加搜索栏参加活动.设置如下:

I've followed this Android guide in order to add a search bar to an activity. The setup looks like this:

res/menu/activity_main.xml:

res/menu/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/search"
    android:title="@string/menu_search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />
<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/menu_settings"
    android:icon="@drawable/ic_menu_settings"/>

</menu>

res/xml/searchable.xml:

res/xml/searchable.xml:

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

清单文件中的活动条目:

Activity entry in manifest file:

    <activity
        android:name="de.ivu.realtime.app.activity.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <meta-data
            android:name="android.app.default_searchable"
            android:value="de.ivu.realtime.app.activity.MainActivity" />
    </activity>

和MainActivity中的代码:

and the code in MainActivity:

@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    this.getMenuInflater().inflate(R.menu.activity_main, menu);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(searchableInfo);
    }

    return true;

searchManager.getSearchableInfo(getComponentName());的调用总是返回null.我在关于stackoverflow的其他问题中已经看到了这一点,但是,那里提出的解决方案似乎都不适用于我的情况.

The call to searchManager.getSearchableInfo(getComponentName()); always returns null. I've seen this in other questions on stackoverflow, however, none of the solutions presented there seem to apply to my case.

  • 菜单XML具有<?xml version="1.0" encoding="utf-8"?>标头
  • 可搜索的XML没有硬编码的字符串
  • 重新命名菜单项不起作用
  • 清理和重建也无法解决问题
  • the menu XML has a <?xml version="1.0" encoding="utf-8"?> header
  • the searchable XML has no hard-coded strings
  • renaming the menu item does not work
  • cleaning and rebuilding does not do the trick either

非常感谢您的帮助.谢谢!

Any help is very much appreciated. Thanks!

推荐答案

将其添加到您放置SearchView的活动中

Add it to the activity where you've put your SearchView

<meta-data
    android:name="android.app.default_searchable"
    android:value=".activity_that_handles_the_search" />

<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>

这篇关于searchManager.getSearchableInfo(getComponentName())返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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