我是Android内容提供商的新手。我正在使用内容提供程序构建一个简单的android应用程序。每次我将数据插入数据库时,都会收到错误 IllegalArgumentException未知的URL内容。
我的logcat错误
07-16 14:50:53.624: E/AndroidRuntime(32763): FATAL EXCEPTION: main
07-16 14:50:53.624: E/AndroidRuntime(32763): Process: com.example.videouploadclient, PID: 32763
07-16 14:50:53.624: E/AndroidRuntime(32763): java.lang.IllegalArgumentException: Unknown URL Content://com.example.videouploadclient.model.provider/video_table
07-16 14:50:53.624: E/AndroidRuntime(32763): at android.content.ContentResolver.insert(ContentResolver.java:1203)
07-16 14:50:53.624: E/AndroidRuntime(32763): at com.example.videouploadclient.view.MainActivity$1.onClick(MainActivity.java:59)
07-16 14:50:53.624: E/AndroidRuntime(32763): at android.view.View.performClick(View.java:4780)
07-16 14:50:53.624: E/AndroidRuntime(32763): at android.view.View$PerformClick.run(View.java:19866)
07-16 14:50:53.624: E/AndroidRuntime(32763): at android.os.Handler.handleCallback(Handler.java:739)
07-16 14:50:53.624: E/AndroidRuntime(32763): at android.os.Handler.dispatchMessage(Handler.java:95)
07-16 14:50:53.624: E/AndroidRuntime(32763): at android.os.Looper.loop(Looper.java:135)
07-16 14:50:53.624: E/AndroidRuntime(32763): at android.app.ActivityThread.main(ActivityThread.java:5254)
07-16 14:50:53.624: E/AndroidRuntime(32763): at java.lang.reflect.Method.invoke(Native Method)
07-16 14:50:53.624: E/AndroidRuntime(32763): at java.lang.reflect.Method.invoke(Method.java:372)
07-16 14:50:53.624: E/AndroidRuntime(32763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-16 14:50:53.624: E/AndroidRuntime(32763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-16 14:50:53.624: E/AndroidRuntime(32763): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
我将数据插入数据库的代码
Uri uri;
ContentValues contentValues = new ContentValues();
contentValues.put(VideoEntry.COLUMN_ID, 111);
contentValues.put(VideoEntry.COLUMN_TITLE, "my title");
contentValues.put(VideoEntry.COLUMN_DURATION, 9876);
contentValues.put(VideoEntry.COLUMN_DATA_URL, "");
contentValues.put(VideoEntry.COLUMN_CONTENT_TYPE, "video/mp4");
contentValues.put(VideoEntry.COLUMN_STAR_RATING, 4);
uri = getContentResolver().insert(VideoEntry.VIDEO_CONTENT_URI, contentValues);
我的合同类
public class VideoContract {
public static final String AUTHORITY = "com.example.videouploadclient.model.provider";
private static final Uri BASE_URI = Uri.parse("Content://" + AUTHORITY);
public static final class VideoEntry implements BaseColumns {
public static String VIDEO_TABLE_NAME = "video_table";
public static final Uri VIDEO_CONTENT_URI = BASE_URI.buildUpon()
.appendPath(VIDEO_TABLE_NAME)
.build();
// table columns
public static final String COLUMN_ID = "id";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DURATION = "duration";
public static final String COLUMN_CONTENT_TYPE = "content_type";
public static final String COLUMN_DATA_URL = "data_url";
public static final String COLUMN_STAR_RATING = "star_rating";
public static Uri buildRowAccessUri (long id) {
return ContentUris.withAppendedId(VIDEO_CONTENT_URI, id);
}
}
}
我的数据库助手类
public class VideoDatabaseHelper extends SQLiteOpenHelper {
private static String DATABASE_NAME = "video_upload_client_database";
private static int DATABASE_VERSION = 1;
private static final String CREATE_TABLE_VIDEO =
"CREATE TABLE " + VideoEntry.VIDEO_TABLE_NAME
+ "(" + VideoEntry.COLUMN_ID
+ " INTEGER PRIMARY KEY, "
+ VideoEntry.COLUMN_TITLE
+ " TEXT, "
+ VideoEntry.COLUMN_DURATION
+ " INT, "
+ VideoEntry.COLUMN_CONTENT_TYPE
+ " TEXT, "
+ VideoEntry.COLUMN_DATA_URL
+ " TEXT, "
+ VideoEntry.COLUMN_STAR_RATING
+ " INT)";
public VideoDatabaseHelper(Context context) {
super(context,
context.getCacheDir()
+ File.separator
+ DATABASE_NAME,
null,
DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_VIDEO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "
+ VideoEntry.VIDEO_TABLE_NAME);
onCreate(db);
}
}
我的内容提供商类
public class VideoProvider extends ContentProvider {
public static final int VIDEO_ITEMS = 100;
public static final int VIDEO_ITEM = 101;
public static final UriMatcher sUriMatcher = buildUriMatcher();
public static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(VideoContract.AUTHORITY, VideoEntry.VIDEO_TABLE_NAME, VIDEO_ITEMS);
matcher.addURI(VideoContract.AUTHORITY, VideoEntry.VIDEO_TABLE_NAME, VIDEO_ITEM);
return matcher;
}
private static final String VIDEO_TABLE_NAME = VideoEntry.VIDEO_TABLE_NAME;
private VideoDatabaseHelper mDatabaseHelper;
@Override
public boolean onCreate() {
mDatabaseHelper = new VideoDatabaseHelper(getContext());
return true;
}
private static String addKeyIdCheckToWhereStatement (String whereStatement, long id) {
String newWhereStatement;
if (TextUtils.isEmpty(whereStatement))
newWhereStatement = "";
else
newWhereStatement = whereStatement + " AND ";
return newWhereStatement + " _id = "
+ "'" + id + "'";
}
@Override
public Cursor query(Uri uri, String[] projection, String whereStatement,
String[] selectionArgs, String sortOrder) {
final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
switch (sUriMatcher.match(uri)) {
case VIDEO_ITEMS:
queryBuilder.setTables(VIDEO_TABLE_NAME);
break;
case VIDEO_ITEM:
queryBuilder.setTables(VIDEO_TABLE_NAME);
whereStatement = addKeyIdCheckToWhereStatement(whereStatement, ContentUris.parseId(uri));
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
final Cursor cursor = queryBuilder.query(mDatabaseHelper.getReadableDatabase(),
projection, whereStatement, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
String table;
Uri returnUri;
switch (sUriMatcher.match(uri)) {
case VIDEO_ITEMS:
table = VideoEntry.VIDEO_TABLE_NAME;
returnUri = VideoEntry.VIDEO_CONTENT_URI;
break;
default:
throw new IllegalArgumentException("Unknown URI "
+ uri);
}
final long insertRow = mDatabaseHelper.getWritableDatabase().insert(table, null, values);
if (insertRow > 0) {
Uri newUri = ContentUris.withAppendedId(returnUri, insertRow);
// Register to watch a content URI for changes.
getContext().getContentResolver().notifyChange(newUri,
null);
return newUri;
} else {
throw new SQLException("Fail to add a new record into "
+ uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int rowsDeleted;
final SQLiteDatabase database = mDatabaseHelper.getWritableDatabase();
switch (sUriMatcher.match(uri)) {
case VIDEO_ITEMS:
rowsDeleted = database.delete(VIDEO_TABLE_NAME, selection, selectionArgs);
break;
case VIDEO_ITEM:
rowsDeleted = database.delete(VIDEO_TABLE_NAME,
addKeyIdCheckToWhereStatement(selection, ContentUris.parseId(uri)), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI "
+ uri);
}
// Register to watch a content URI for changes.
getContext().getContentResolver().notifyChange(uri,
null);
return rowsDeleted;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int rowsUpdated;
final SQLiteDatabase database = mDatabaseHelper.getWritableDatabase();
switch (sUriMatcher.match(uri)) {
case VIDEO_ITEMS:
rowsUpdated = database.update(VIDEO_TABLE_NAME, values, selection, selectionArgs);
break;
case VIDEO_ITEM:
rowsUpdated = database.update(VIDEO_TABLE_NAME, values, addKeyIdCheckToWhereStatement(selection, ContentUris.parseId(uri)), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI "
+ uri);
}
// Register to watch a content URI for changes.
getContext().getContentResolver().notifyChange(uri,
null);
return rowsUpdated;
}
}
我不知道我的代码有什么问题。我搜索了许多相关主题,没有任何答案。
答案 0 :(得分:1)
buildUriMatcher()
出错。您添加的两个URI都具有相同的路径VideoContract.VideoEntry.VIDEO_TABLE_NAME
。我认为第二个替换了第一个,因此在该路径的匹配上返回的代码是VIDEO_ITEM
。在insert()
方法中,VIDEO_ITEM
没有任何情况,因此它执行默认处理,即抛出堆栈跟踪中报告的IllegalArgumentException
。
public static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(VideoContract.AUTHORITY, VideoContract.VideoEntry.VIDEO_TABLE_NAME, VIDEO_ITEMS);
matcher.addURI(VideoContract.AUTHORITY, VideoContract.VideoEntry.VIDEO_TABLE_NAME, VIDEO_ITEM);
return matcher;
}