0

我在匹配ContentProvider. 它将匹配带有一个或两个“标签”的路径(不确定正确的术语是什么),例如它可以识别box/#. 但是,一旦我添加了第二个标签,例如box/#/item,我抛出一个InvalidArgumentException并且我的应用程序崩溃了。我在这里读过几个线程,有时可以通过更改添加到UriMatcher;的 URI 的顺序来解决这个问题。我试过这个,但没有用。有谁知道问题是什么?我在下面摘录了我的代码的相关部分。

从我的ContentProvider

private static final String AUTHORITY = "com.example.boxdatabase.DatabaseProvider";
// Type of query
public static final int BOXES = 100;
    ...

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

// URI Matcher for queries
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
    sURIMatcher.addURI(AUTHORITY, "box/#/item", BOX_ITEM);
    sURIMatcher.addURI(AUTHORITY, "box/#/item/#", BOX_ITEM_ID);
    sURIMatcher.addURI(AUTHORITY, "box", BOXES);
    sURIMatcher.addURI(AUTHORITY, "box/#", BOXES_ID);
    sURIMatcher.addURI(AUTHORITY, "item", ITEMS);
    sURIMatcher.addURI(AUTHORITY, "item/#", ITEMS_ID);
}

我尝试访问我ContentProvider但失败的示例:

return new CursorLoader(this, Uri.withAppendedPath(
                DatabaseProvider.CONTENT_URI, "box/" + boxId + "/item"),
                DatabaseContract.BoxItemEntry.ALL_COLUMNS, null, null, null);
4

1 回答 1

4

尝试这个:

sURIMatcher.addURI(AUTHORITY, "box", BOXES);
sURIMatcher.addURI(AUTHORITY, "box/#", BOXES_ID);
sURIMatcher.addURI(AUTHORITY, "box/#/item", BOX_ITEM);
sURIMatcher.addURI(AUTHORITY, "box/#/item/#", BOX_ITEM_ID);
sURIMatcher.addURI(AUTHORITY, "item", ITEMS);
sURIMatcher.addURI(AUTHORITY, "item/#", ITEMS_ID);

顺序很重要,请在此处阅读我的解释: https ://stackoverflow.com/a/15015687/534471

于 2013-06-03T01:30:07.850 回答