打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android – UriMatcher与uri不匹配

我正在尝试对搜索对话框进行自定义建议.我正在使用urimatcher来匹配uri.但它不起作用.我总是得到异常“java.lang.IllegalArgumentException:Unknown Uri:content://com.simple.search.SuggestionProvider/search_suggest_query/?limit = 50”.请向我解释一下.我该怎么做才能解决这个问题?

private static final UriMatcher sURIMatcher = makeUriMatcher();public Cursor query(Uri uri, String[] projection, String selection,        String[] selectionArgs, String sortOrder) {    // Use UriMatcher, to find out what type of request received. Next, form    // the corresponding query to the database    switch (sURIMatcher.match(uri)) {    case SEARCH_SUGGEST:        if (selectionArgs == null) {            throw new IllegalArgumentException(                    "selectionArgs must be provided for the Uri: "   uri);        }        return getSuggestions(selectionArgs[0]);    case SEARCH_TESTS:        if (selectionArgs == null) {            throw new IllegalArgumentException(                    "selectionArgs must be provided for the Uri: "   uri);        }        return search(selectionArgs[0]);    case GET_TEST:        return getRecord(uri);    default:        throw new IllegalArgumentException("Unknown Uri: "   uri);    }

makeUrimatcher

private static UriMatcher makeUriMatcher() {    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);    // For the record    matcher.addURI(AUTHORITY, "tests", SEARCH_TESTS);    matcher.addURI(AUTHORITY, "tests/#", GET_TEST);    // For suggestions table    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,            SEARCH_SUGGEST);    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY   "/*",            SEARCH_SUGGEST);    return matcher;}

logcat的

11-30 14:16:27.295: I/ActivityThread(1638): Publishing provider com.simple.search.SuggestionProvider: com.simple.search.SuggestionProvider11-30 14:16:35.424: D/com.simple.search.com.simple.search.SuggestionProvider(1638): Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=5011-30 14:16:35.424: E/DatabaseUtils(1638): Writing exception to parcel11-30 14:16:35.424: E/DatabaseUtils(1638): java.lang.IllegalArgumentException: Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=5011-30 14:16:35.424: E/DatabaseUtils(1638):  at com.simple.search.SuggestionProvider.query(SuggestionProvider.java:122)11-30 14:16:35.424: E/DatabaseUtils(1638):  at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:117)11-30 14:16:35.424: E/DatabaseUtils(1638):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:98)11-30 14:16:35.424: E/DatabaseUtils(1638):  at android.os.Binder.execTransact(Binder.java:287)11-30 14:16:35.424: E/DatabaseUtils(1638):  at dalvik.system.NativeStart.run(Native Method)

searchable.xml

<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"    android:hint="@string/search_hint"     android:label="@string/app_label"    android:searchSuggestAuthority="com.simple.search.SuggestionProvider"    android:searchSuggestIntentAction="android.intent.action.VIEW"    android:searchSuggestIntentData="content://com.simple.search.SuggestionProvider/tests"    />

表现

........        <activity            android:label="@string/app_name"            android:name=".SimpleSearch" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.SEARCH" />            </intent-filter>                <meta-data android:name="android.app.searchable"                   android:resource="@xml/searchable"/>        </activity>........

解决方法:

为避免讨论,如果您提供更多信息,我将更改此答案…

但现在 …

xml中有android:searchSuggestIntentData =“content://com.simple.search.SuggestionProvider/tests”

所以你必须改变

private static UriMatcher makeUriMatcher() {    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);    // For the record    matcher.addURI(AUTHORITY, "tests", SEARCH_TESTS);    matcher.addURI(AUTHORITY, "tests/#", GET_TEST);    // For suggestions table    matcher.addURI(AUTHORITY, "tests/"   SearchManager.SUGGEST_URI_PATH_QUERY,            SEARCH_SUGGEST);    matcher.addURI(AUTHORITY, "tests/"   SearchManager.SUGGEST_URI_PATH_QUERY   "/*",            SEARCH_SUGGEST);    return matcher;}

如果你没有看到差异我添加“测试/”

所以现在它将匹配内容://com.simple.search.SuggestionProvider/tests/search_suggest_query?limit = 50这正是QSB将发送的内容……

无论如何,你可以/应该为你的查询添加限制

case SEARCH_SUGGEST:    if (selectionArgs == null) {        throw new IllegalArgumentException(                "selectionArgs must be provided for the Uri: "   uri);    }    final String limit = uri.getQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT);    return getSuggestions(selectionArgs[0], limit);

然后在getSuggestions中

helper.getReadableDatabase().query(table, projection,                    selection, selectionArgs, null, null, sortOrder, limit);

编辑:

AUTHORITY“tests /”SearchManager.SUGGEST_URI_PATH_QUERY应该与android:searchSuggestIntentData相同!!!

EDIT2:来自doc http://developer.android.com/guide/topics/search/adding-custom-suggestions.html

selection The value provided in the android:searchSuggestSelection
attribute of your searchable configuration file, or null if you have
not declared the android:searchSuggestSelection attribute. More about
using this to get the query below. selectionArgs Contains the search
query as the first (and only) element of the array if you have
declared the android:searchSuggestSelection attribute in your
searchable configuration. If you have not declared
android:searchSuggestSelection, then this parameter is null. More
about using this to get the query below.

添加android:searchSuggestSelection =“?”

来源:https://www.icode9.com/content-4-269451.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
android之ContentResolver与ContentProvider
ContentProvider数据库共享之
UriMatcher
【Android】Uri、UriMatcher、ContentUris详解
ContentProvider使用详解
Android内容提供者(Content provider)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服