为什么我的ContentProvider未知?

时间:2011-11-18 18:19:26

标签: android cursor uri android-manifest android-contentprovider

的AndroidManifest.xml

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

<manifest ... >部分

 <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

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

1 个答案:

答案 0 :(得分:3)

您的代码:

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");