ContentProvider提供Unknown URI错误

时间:2012-08-09 05:23:57

标签: android null android-contentprovider

我已经创建了自己的ContentProvider,但我不确定如何检索它。我在调用getType方法时一直得到null。

这是我的代码:
CurrentPlacesMetaData类:

public static final String AUTHORITY = "healthcare.management.elderly.providers.CurrentPlacesContentProvider";
public static final String CONTENT_TYPE_PLACES_LIST = "vnd.android.cursor.dir/vnd.healthcare.places";
public static final String CONTENT_TYPE_PLACES_ONE = "vnd.android.cursor.dir/vnd.healthcare.places#";

CurrentPlacesContentProvider类:

public static final Uri CONTENT_URI = Uri.parse("content://" 
        + CurrentPlacesMetaData.AUTHORITY + "/currentplaces");
static {
     sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
     sUriMatcher.addURI(CurrentPlacesMetaData.AUTHORITY, "places", PLACES_TYPE_LIST);
     sUriMatcher.addURI(CurrentPlacesMetaData.AUTHORITY, "places/#", PLACES_TYPE_ONE);
}
@Override
public String getType(Uri uri) {
    switch(sUriMatcher.match(uri)) {
        case PLACES_TYPE_LIST:
            return CurrentPlacesMetaData.CONTENT_TYPE_PLACES_LIST;
        case PLACES_TYPE_ONE:
            return CurrentPlacesMetaData.CONTENT_TYPE_PLACES_ONE;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }
}

尝试调用ContentProvider的方法:

Uri uri = Uri.parse("content://healthcare.management.elderly.providers.CurrentPlacesContentProvider/currentplaces/places");
String str = context.getContentResolver().getType(uri)

我得到str的null,这可能告诉uri没有被识别。那么我哪里出错了? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用

调用ContentProvider
Cursor c = managedQuery(uri, null, null, null, null);

但由于现在不推荐使用managedQuery,因此必须使用CursorLoader。以下是使用CursorLoader的示例。

答案 1 :(得分:1)

您的URI包含currentplaces作为最后一部分,而在uri matcher中您匹配 places

您可以使用以下格式进行内容提供商的uri匹配

private static final String AUTHORITY = "xxxxxxxxxxxxx";
private static final String COMMENT_BASE_PATH = "comment";

private static final int COMMENT = 100;
private static final int COMMENT_ID = 101;


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

private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
    sURIMatcher.addURI(AUTHORITY, COMMENT_BASE_PATH , COMMENT);
    sURIMatcher.addURI(AUTHORITY, COMMENT_BASE_PATH + "/*" , COMMENT_ID);
}

然后你可以在任何你喜欢的地方推荐 URI_COMMENT