Reputation: 10830
This is what I have, but its failing saying that the table does not exists. I am positive it does incase anyone asks that. But this is some of the code that does that:
What gets called
/*Constants*/
public static final String AUTHORITY = "content://com.smartcal.eventprovider";
private static final int EVENTS_INFO = 1;
baseUri = Uri.withAppendedPath(baseUri, "events_info");
return new CursorLoader(this, baseUri, args.getStringArray("projection"),
args.getString("selection"), args.getStringArray("selectionArgs"), args.getBoolean("sortOrder") ? args.getString("sortOrder") : null );
What matches is
private String getTable(Uri uri) {
String table = "";
switch(sUriMatcher.match(uri)){
case EVENTS_INFO: table = "events_info";
}
return table;
}
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static
{
sUriMatcher.addURI(AUTHORITY, "events_info", 1);
}
And my query()
method in custom ContentProvider
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
database = openHelper.getWritableDatabase();
return database.query(getTable(uri), projection, selection, selectionArgs, null, null, null);
}
Error:
06-19 17:34:14.366: E/AndroidRuntime(686): Caused by:
java.lang.IllegalStateException: Invalid tables
EDIT:
When using Alex's IllegalArgumentException
, this is what I got back from the error:
06-19 19:17:42.277: E/AndroidRuntime(1134): Caused by:
java.lang.IllegalArgumentException: Unknown URI
content://com.smartcal.eventprovider/events_info
As you can tell, it should match, assuming the sUriMatcher.addURI()
method is working correctly.
Upvotes: 0
Views: 1467
Reputation: 83303
Your problem might stem from the fact that your code isn't very well organized...
First thing,
private String getTable(Uri uri) {
switch(sUriMatcher.match(uri)) {
case EVENTS_INFO:
return "events_info"; // return
/** PROVIDE A DEFAULT CASE HERE **/
default:
// If the URI doesn't match any of the known patterns, throw an exception.
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
Second thing,
/** Use constants, not raw ints **/
sUriMatcher.addURI(AUTHORITY, "events_info", EVENT_INFO);
Other than that, you might want to provide some more info.
Upvotes: 2
Reputation: 4168
Have you already installed the app and is the table new? If so, and you are not updating the schema in your sql helper class, you need to uninstall the app first. Then install the app in your emulator device. The db is only created once, and the create method in the sql helper will not be called after that first time, even if you are installing or running a new build/apk.
Upvotes: 0