为什么我的 ContentProvider 未知?

发布于 2024-12-17 00:35:51 字数 1755 浏览 0 评论 0原文

AndroidManifest.xml 的

 <!-- Provides search suggestions for addresses -->
 <provider android:name="com.my.app.provider.SearchAddressProvider"
              android:authorities="com.my.app.provider.SearchAddressProvider" />

部分

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app" android:versionCode="1"
android:versionName="1.0">

来自我的提供程序类 SearchAddressProvider

 public static final String KEY_ADDRESS_1 = SearchManager.SUGGEST_COLUMN_TEXT_1;
 public static final String KEY_ADDRESS_2 = SearchManager.SUGGEST_COLUMN_TEXT_2;
 public static final String KEY_MARKER = SearchManager.SUGGEST_COLUMN_ICON_1;
 public static final String KEY_FAVORITE = SearchManager.SUGGEST_COLUMN_ICON_2;

 private static String AUTHORITY = "com.my.app.provider.SearchAddressProvider";
 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/searchaddress");
 private static final UriMatcher uriMatcher = buildUriMatcher();

 ....

 @Override
public String getType(Uri uri) {
    switch (uriMatcher.match(uri)) {
    case SEARCH_WORDS:
        return WORDS_MIME_TYPE;
    case GET_WORD:
        return DEFINITION_MIME_TYPE;
    case SEARCH_SUGGEST:
        return SearchManager.SUGGEST_MIME_TYPE;
    default:
        throw new IllegalArgumentException("Unknown URL " + uri);
    }
}

但在我的活动中,使用这些定义调用此类,使用以下命令:

 Cursor cursor = managedQuery(SearchAddressProvider.CONTENT_URI, null, null, new String[] {query}, null);

我得到IllegalArgumentException Unknown URI

这对我来说是个问题,解决方案吗?

AndroidManifest.xml

 <!-- Provides search suggestions for addresses -->
 <provider android:name="com.my.app.provider.SearchAddressProvider"
              android:authorities="com.my.app.provider.SearchAddressProvider" />

the <manifest ... > section

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app" android:versionCode="1"
android:versionName="1.0">

From my provider class SearchAddressProvider

 public static final String KEY_ADDRESS_1 = SearchManager.SUGGEST_COLUMN_TEXT_1;
 public static final String KEY_ADDRESS_2 = SearchManager.SUGGEST_COLUMN_TEXT_2;
 public static final String KEY_MARKER = SearchManager.SUGGEST_COLUMN_ICON_1;
 public static final String KEY_FAVORITE = SearchManager.SUGGEST_COLUMN_ICON_2;

 private static String AUTHORITY = "com.my.app.provider.SearchAddressProvider";
 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/searchaddress");
 private static final UriMatcher uriMatcher = buildUriMatcher();

 ....

 @Override
public String getType(Uri uri) {
    switch (uriMatcher.match(uri)) {
    case SEARCH_WORDS:
        return WORDS_MIME_TYPE;
    case GET_WORD:
        return DEFINITION_MIME_TYPE;
    case SEARCH_SUGGEST:
        return SearchManager.SUGGEST_MIME_TYPE;
    default:
        throw new IllegalArgumentException("Unknown URL " + uri);
    }
}

yet in my activity that calls this class with those definitions, with this command:

 Cursor cursor = managedQuery(SearchAddressProvider.CONTENT_URI, null, null, new String[] {query}, null);

I get IllegalArgumentException Unknown URI

thats a problem for me, solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怂人 2024-12-24 00:35:51

您的代码:

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                    + "/searchaddress");

/**
 * Builds up a UriMatcher for search suggestion and shortcut refresh
 * queries.
 */
private static UriMatcher buildUriMatcher() {
    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    // to get definitions...
    matcher.addURI(AUTHORITY, "dictionary", SEARCH_WORDS);
    matcher.addURI(AUTHORITY, "dictionary/#", GET_WORD);
    // to get suggestions...
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,
                    SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
                    SEARCH_SUGGEST);

    return matcher;
}

请注意,您的 UriMatcher 不支持 searchaddress,而是处理 dictionary。所以我认为 CONTENT_URI 应该更改为:

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + 
         "/dictionary");

Your code:

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                    + "/searchaddress");

/**
 * Builds up a UriMatcher for search suggestion and shortcut refresh
 * queries.
 */
private static UriMatcher buildUriMatcher() {
    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    // to get definitions...
    matcher.addURI(AUTHORITY, "dictionary", SEARCH_WORDS);
    matcher.addURI(AUTHORITY, "dictionary/#", GET_WORD);
    // to get suggestions...
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,
                    SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
                    SEARCH_SUGGEST);

    return matcher;
}

Note, your UriMatcher does not support searchaddress, it deals with dictionary instead. So I think the CONTENT_URI should be changed to:

public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + 
         "/dictionary");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文