UriMatcher不正确地匹配 [英] UriMatcher doesn't match correctly

查看:672
本文介绍了UriMatcher不正确地匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一项:我经历了许多对等的话题提问过筛,并仍然未能拿出一个正确的答案

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.

下面是一个(简单的方式),我的code版本:

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;
}

我有我的课的要求或请求的设置没有实现所以这就是为什么我只是默认返回。这是怎么回事是语句是通过降一路我的开关置于默认值:,后来就造成NPE的嘉豪在我code。你会注意到,我在code 5 登录语句。以下是打印到LogCat中。 (为什么叫logcat的反正?)

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

现在正在测​​试的URI应该匹配第二模式我加了,对不对?我也了解如何使用 Uri.parse() UriMatcher 方法混乱的另一个线程,所以我打造的通配符上面印乌里通过以下code:

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

ID 变量存在取决于是不是真的相关的一些其他的东西。

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

我的问题是,为什么是 UriMatcher 不工作,我将如何去修复它?

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

我有不正确的大写以下行:

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

通知 homeworkreminder 小写研究。感谢布鲁斯为察觉呢!

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

推荐答案

您使用的是小写的提醒,当你设置了匹配,但实际URI有大写的'提醒'。

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

这篇关于UriMatcher不正确地匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆