URIMatcher不匹配

用户672009

这工作了!我不确定发生了什么变化。我确实使用git,并且已经检查了我的提交和代码几个小时了。

如标题所示,我的uri匹配器已停止匹配。

下面,我粘贴了内容提供商的相关部分。

public class Provider extends ContentProvider {
    private static final String TAG = "Provider";
    private static final String SCHEME = ContentResolver.SCHEME_CONTENT;
    private static final String AUTHORITY = "com.snot.bodyweightworkout.database.provider";
    private static final String BASE_URI = SCHEME + AUTHORITY;

    public static final Uri URI_EXERCISES = Uri.parse(BASE_URI + "/exercise");
    public static final Uri URI_PROGRAMS = Uri.parse(BASE_URI + "/program");
    public static final Uri URI_PROGRAM_EXERCISES = Uri.parse(BASE_URI + "/program/#/exercise");

    private static final int EXERCISE = 1;
    private static final int EXERCISES = 2;
    private static final int PROGRAM = 3;
    private static final int PROGRAMS = 4;
    private static final int PROGRAM_EXERCISES = 5;

    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static
    {
        sURIMatcher.addURI(AUTHORITY, "/exercise", EXERCISES);
        sURIMatcher.addURI(AUTHORITY, "/exercise/#", EXERCISE);
        sURIMatcher.addURI(AUTHORITY, "/program", PROGRAMS);
        sURIMatcher.addURI(AUTHORITY, "/program/#", PROGRAM);
        sURIMatcher.addURI(AUTHORITY, "/program/#/exercise", PROGRAM_EXERCISES);
    }

...

然后是实际匹配的部分。

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Log.v(TAG, "URI: " + uri);
    Cursor result = null;
    int match = sURIMatcher.match(uri);
    switch(match)
    {
        case PROGRAMS:
            result = DatabaseHandler
                .getInstance(getContext())
                .getReadableDatabase()
                .query(Program.TABLE_NAME, Program.FIELDS, null, null, null, null, null, null);
            result.setNotificationUri(getContext().getContentResolver(), URI_PROGRAMS);
            break;
        case PROGRAM:
            final long pid = Long.parseLong(uri.getLastPathSegment());
            result = DatabaseHandler
                .getInstance(getContext())
                .getReadableDatabase()
                .query(Program.TABLE_NAME, Program.FIELDS,
                        Program.COL_ID + " IS ?",
                        new String[] { String.valueOf(pid) }, null, null, null, null);
            result.setNotificationUri(getContext().getContentResolver(), URI_PROGRAMS);
            break;
       ...
        default:
            throw new UnsupportedOperationException("Unmatched(" + match + ") URI: " + uri.toString());

我正在尝试使用游标加载器进行查询,如下所示:

     getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             return new CursorLoader(getActivity(), Provider.URI_PROGRAMS, Program.FIELDS, null, null, null);
         }

每次都点击默认值。不匹配导致我以FC和日志中的以下行结尾。

E/AndroidRuntime( 1979): Caused by: java.lang.UnsupportedOperationException: Unmatched(-1) URI: content://com.snot.bodyweightworkout.database.provider/program

我一直在盯着这个看了几个小时,我真的需要一些新鲜的眼睛才能达到顶峰。因此,如果有一个善良的灵魂可以看一下,我将非常感激。提前致谢。

用户

不需要启动/,因此可以这样定义您的addUri()方法调用:

static {
   sURIMatcher.addURI(AUTHORITY, "exercise", EXERCISES);
   sURIMatcher.addURI(AUTHORITY, "exercise/#", EXERCISE);
   sURIMatcher.addURI(AUTHORITY, "program", PROGRAMS);
   sURIMatcher.addURI(AUTHORITY, "program/#", PROGRAM);
   sURIMatcher.addURI(AUTHORITY, "program/#/exercise", PROGRAM_EXERCISES);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章