Android – urimatcher mismatch

It works! I'm not sure what has changed. I do use GIT. I've been looking at my submission and code for hours

Because the title indicates that my URI matcher stopped matching

Below I pasted the relevant section of my content provider

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

Then there is the part that should be actually matched

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

I'm trying to query with a cursor loader like this:

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

Every violation will be hit. The mismatch is that I finally use FC and the following lines in my log

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

I've been starring here for several hours. I really need some fresh eyes to aim at it. Therefore, if some soul can take a look, I will be very grateful. Thank you in advance

There is no need to start /, so define your adduri () method call as follows:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>