Android内容提供商IllegalArgumentException未知URL内容

Android内容提供商IllegalArgumentException未知URL内容,android,database,sqlite,android-contentprovider,illegalargumentexception,Android,Database,Sqlite,Android Contentprovider,Illegalargumentexception,我是Android内容提供商的新手。我正在使用内容提供商构建一个简单的android应用程序。每次我将数据插入数据库时,都会出现错误IllegalArgumentException未知URL内容 我的日志错误 07-16 14:50:53.624: E/AndroidRuntime(32763): FATAL EXCEPTION: main 07-16 14:50:53.624: E/AndroidRuntime(32763): Process: com.example.videouploadc

我是Android内容提供商的新手。我正在使用内容提供商构建一个简单的android应用程序。每次我将数据插入数据库时,都会出现错误IllegalArgumentException未知URL内容

我的日志错误

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

}

我不知道我的代码出了什么问题。我搜索了许多相关的话题,没有找到答案

buildUriMatcher()中有一个错误。
。您添加的两个URI都有相同的路径,
VideoContract.VideoEntry.VIDEO\u TABLE\u NAME
。我认为第二个替代了第一个,因此在匹配该路径时返回的代码是
VIDEO\u ITEM
。在您的
insert()
方法中,
VIDEO\u项
没有任何情况,因此它执行默认处理,即抛出堆栈跟踪中报告的
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;
}

对于这个特定的bug,所有的代码都是必需的吗?清理代码并缩短其长度以获得更好更快的帮助。我认为最好在我不知道哪里出错时显示所有相关代码。这是一个问题:-)我不知道答案,也不知道问题所在。但是我总是建议大家,当一篇文章有很多代码时,哪一段代码才是真正有用的。你可以使用:matcher.addURI(VideoContract.AUTHORITY,VideoContract.VideoEntry.VIDEO_TABLE_NAME+“/#”,VIDEO_ITEM_WITH_ID);