UriMatcher doesn't match correctly

流过昼夜 提交于 2019-12-24 13:46:08

问题


First up: I have sifted through many of the questions on the topic on SO and have still failed to come up with a correct answer.

Here is a, (way simplified), version of my code:

private static UriMatcher
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

private static final int
        HOMEWORK_TABLE_REQUEST = 1,
        CLASS_TABLE_REQUEST = 2,
        SETTINGS_TABLE_REQUEST = 3,
        HOMEWORK_ITEM_REQUEST = 4,
        CLASS_ITEM_REQUEST = 5,
        SETTINGS_ITEM_REQUEST = 6;

static {
    uriMatcher.addURI("org.dvc.homeworkReminder.homeworkProvider", "homework", HOMEWORK_TABLE_REQUEST);
    uriMatcher.addURI("org.dvc.homeworkreminder.homeworkProvider", "homework/#", HOMEWORK_ITEM_REQUEST);
    uriMatcher.addURI("org.dvc.homeworkReminder.homeworkProvider", "class", CLASS_TABLE_REQUEST);
    uriMatcher.addURI("org.dvc.homeworkreminder.homeworkProvider", "class/#", CLASS_ITEM_REQUEST);
    uriMatcher.addURI("org.dvc.homeworkReminder.homeworkProvider", "settings", SETTINGS_TABLE_REQUEST);
    uriMatcher.addURI("org.dvc.homeworkreminder.homeworkProvider", "settings/#", SETTINGS_ITEM_REQUEST);
}

And here is my query method:

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor cursor = null;   
    Log.i("dfg", "Query method called");
    Log.i("dfg", "Uri = " + uri.toString());
    Log.i("dfg", "Match: " + uriMatcher.match(uri));
    switch(uriMatcher.match(uri)) {
        case HOMEWORK_ITEM_REQUEST:
            Log.i("dfg", "HOMEWORK_ITEM_REQUEST");
            cursor = database.readCursorById(Integer.parseInt(uri.getLastPathSegment()));
            break;
        case HOMEWORK_TABLE_REQUEST:
            Log.i("dfg", "HOMEWORK_TABLE_REQUEST");
            cursor = database.readAllHomework();
            break;
        case CLASS_ITEM_REQUEST:
        case CLASS_TABLE_REQUEST:
        case SETTINGS_ITEM_REQUEST:
        case SETTINGS_TABLE_REQUEST:
            cursor = null;
            break;
        default:
            cursor = null;
            break;
    }
    return cursor;
}

I have no implementation for my class requests or settings requests so that's why I'm just defaulting to return null. What's happening is my switch statement is falling all the way through to default:, and causing NPE's galore later on in my code. You'll notice there are 5 Log statements in my code. The following is printed to LogCat. (Why is it called logcat anyways?)

Query method called
Uri = content://org.dvc.homeworkReminder.homeworkProvider/homework/24
Match: -1

Now the uri being tested should match the 2nd pattern I added, correct? I also read about how the Uri.parse() method messes with UriMatcher's wildcards on another thread so I build the above-printed Uri with the following code:

Uri returnUri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority("org.dvc.homeworkReminder.homeworkProvider").appendPath("homework").appendPath(String.valueOf(id)).build();

The id variable there depends on some other stuff that isn't really relevant.

My question is why is UriMatcher not working, and how would I go about fixing it?

Fix:

I had incorrect capitalization on the following lines:

uriMatcher.addURI("org.dvc.homeworkreminder.homeworkProvider", "homework/#", HOMEWORK_ITEM_REQUEST);
uriMatcher.addURI("org.dvc.homeworkreminder.homeworkProvider", "class/#", CLASS_ITEM_REQUEST);
uriMatcher.addURI("org.dvc.homeworkreminder.homeworkProvider", "settings/#", SETTINGS_ITEM_REQUEST);

Notice the lowercase r in homeworkreminder. Thanks to Bruce for spotting it!


回答1:


You're using lower-case 'reminder' when you set up the matcher, but the actual URI has upper-case 'Reminder'.



来源:https://stackoverflow.com/questions/23718100/urimatcher-doesnt-match-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!