urimatcher not working with prepopulated database

I'm trying to add prepopulated SQLITE tables to my content provider using SQLiteAssetHelper but the uri matcher won't match. I can access/modify the tables through standard SQL but using a cursor loader throws an exception. Here is the relevant code in the content provider/cursor loader.

//Content provider code

private PantryDbHelper dbHelper;

private static final int PANTRY = 1;
private static final int INFO = 5;

public static final String AUTHORITY = "com.battlestarmathematica.stayfresh.pantryprovider";

//path to db
public static final String URL = "content://" + AUTHORITY;
public static final Uri CONTENT_URI = Uri.parse(URL);
public static final Uri CONTENT_URI_PANTRY = Uri.withAppendedPath(CONTENT_URI,"pantry");
public static final Uri CONTENT_URI_INFO = Uri.withAppendedPath(CONTENT_URI,"info");

static final UriMatcher uriMatcher;

static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(AUTHORITY,"info",INFO);
    uriMatcher.addURI(AUTHORITY, "pantry", PANTRY);
}

public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder){
    SQLiteDatabase db = dbHelper.getReadableDatabase();

    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        switch (uriMatcher.match(uri)) {
            case PANTRY:
                builder.setTables(PantryContract.PANTRY_TABLE_NAME);
                break;
            case INFO:
                builder.setTables("info");
            default:
                throw new IllegalArgumentException("Unsupported URI " + uri);
        }

//Cursor loader code

public Loader<Cursor> onCreateLoader(int id, Bundle args){
    return new CursorLoader(
            //context
            this,
            //content URI
            PantryContentProvider.CONTENT_URI_INFO,
            //columns to return
            new String[] {"_id","itemname"},
            //selection
            null,
            //selection args
            null,
            //sort order
            "itemname");
}

I know the cursor loader works because I use the exact same code for another activity with the pantry uri and it works perfectly. When I try to load it using the info uri though I get this exception.

java.lang.IllegalArgumentException: Unsupported URI content://com.battlestarmathematica.stayfresh.pantryprovider/info

Any help would be greatly appreciated.


You're just missing a break statement in your code.

The UriMatcher matches and the switch statement jumps to case INFO: , but since there is no break; the default: case is executed as well.

Try to replace this

        case INFO:
            builder.setTables("info");
        default:
            throw new IllegalArgumentException("Unsupported URI " + uri);

by this:

        case INFO:
            builder.setTables("info");
            break;
        default:
            throw new IllegalArgumentException("Unsupported URI " + uri);
链接地址: http://www.djcxy.com/p/31996.html

上一篇: Vpnservice DatagramChannel.open()不起作用

下一篇: urimatcher无法使用预填充的数据库