分享

android – UriMatcher与uri不匹配

 印度阿三17 2019-06-26

我正在尝试对搜索对话框进行自定义建议.我正在使用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.SuggestionProvider
11-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=50
11-30 14:16:35.424: E/DatabaseUtils(1638): Writing exception to parcel
11-30 14:16:35.424: E/DatabaseUtils(1638): java.lang.IllegalArgumentException: Unknown Uri: content://com.simple.search.SuggestionProvider/search_suggest_query/?limit=50
11-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./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./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./content-4-269451.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多