Android:getSearchableInfo(getComponentName())返回空值? [英] Android: getSearchableInfo(getComponentName()) returning null?
问题描述
我正在尝试获取有关SearchView的建议.我已经为其实现了自定义内容提供程序.我还引用了此链接来实现针对SearchView.我面临的问题是,我在searchManager.getSearchableInfo(getComponentName())
I'm trying to get suggestions for SearchView. I've implemented a custom content provider for it. I've also referred to this link to implement suggestions for the SearchView. The problem I'm facing is, I get null value on searchManager.getSearchableInfo(getComponentName())
以下是摘要:
AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<provider android:name=".SearchProvider"
android:enabled="true"
android:authorities="com.example.currentlocationmapdemo"
android:grantUriPermissions="true"
android:exported="true">
<grant-uri-permission android:pathPattern="*" />
</provider>
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<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"/>
</activity>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="label"
android:hint="hint"
android:searchSuggestAuthority="com.example.currentlocationmapdemo"
android:searchSuggestSelection=" ? ">
MainActivity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(getComponentName()); // null returned
mSearchView.setSearchableInfo(info);
return true;
}
推荐答案
您的至少一项活动-您从中进行搜索的活动是足够的-必须在清单中包含以下intent过滤器:>
At least one of your activities - the one you're doing the searching from is sufficient - must have this intent-filter in it in the manifest:
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
如果没有,则searchManager.getSearchableInfo(getComponentName())
始终返回null,并且您的配置将被忽略.
If not, then searchManager.getSearchableInfo(getComponentName())
always returns null, and your configuration is ignored.
这使我丧命了一天-我以为这与ActionBarSherlock有关,但没有效果.问题是,我也试图将样品短路:-)
This killed me for a day - I thought it was ActionBarSherlock-related, but no it works fine with that. The problem was that I was trying to short-circuit the sample, as you have too :-)
这篇关于Android:getSearchableInfo(getComponentName())返回空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!