IllegalArgumentException:未知的URL内容:// CONTENT

时间:2015-03-20 20:58:01

标签: java android android-activity android-contentprovider android-database

IllegalArgumentException:未知的网址内容://

^与上面的一场噩梦。我检查了我的变量和路径,但看不出是什么问题?非常感谢任何指针!

这是我的踪迹。

 java.lang.IllegalArgumentException: Unknown URL     
 content://com.purewowstudio.topmovies.data.FilmProvider/film_data
        at android.content.ContentResolver.insert(ContentResolver.java:1203)
        at com.purewowstudio.topmovies.data.DatabaseHelper.addFilm(DatabaseHelper.java:52)
        at com.purewowstudio.topmovies.fragments.FilmList$getFilms.onPostExecute(FilmList.java:72)
        at com.purewowstudio.topmovies.fragments.FilmList$getFilms.onPostExecute(FilmList.java:62)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5262)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

内容提供商

public class FilmProvider extends ContentProvider {

public static final String TABLE_NAME = "film_data";
public static final String AUTHORITY =  "com.purewowstudio.topmovies.data.FilmProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);

public static final int FILMS = 1;
public static final int FILMS_ID = 2;

public static final UriMatcher sURIMatcher =
        new UriMatcher(UriMatcher.NO_MATCH);

static {
    sURIMatcher.addURI(AUTHORITY, TABLE_NAME, FILMS);
    sURIMatcher.addURI(AUTHORITY, TABLE_NAME + "/#",
            FILMS_ID);
}

private DatabaseHelper mDB;

public boolean onCreate() {
    mDB = new DatabaseHelper(getContext(), null, null, 1);
    return false;
}

@Override
public String getType(Uri uri) {
    return null;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[]        selectionArgs, String sortOrder) {

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(MovieDataContract.TABLE_NAME);
    int uriType = sURIMatcher.match(uri);

    switch (uriType) {
        case FILMS_ID:
            queryBuilder.appendWhere(MovieDataContract.FilmEntry._ID + "="
                    + uri.getLastPathSegment());
            break;
        case FILMS:
            break;
        default:
            throw new IllegalArgumentException("Unknown URI");
    }

    Cursor cursor = queryBuilder.query(mDB.getReadableDatabase(),
            projection, selection, selectionArgs, null, null,
            sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(),
            uri);
    return cursor;

}

@Override
public Uri insert(Uri uri, ContentValues values) {

    int uriType = sURIMatcher.match(uri);

    SQLiteDatabase sqlDB = mDB.getWritableDatabase();

    long id = 0;
    switch (uriType) {
        case FILMS:
            id = sqlDB.insert(MovieDataContract.TABLE_NAME,
                    null, values);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: "
                    + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return Uri.parse(MovieDataContract.TABLE_NAME + "/" + id);
}

DatabaseHelper Class

 public class DatabaseHelper extends SQLiteOpenHelper {

private ContentResolver myCR;

public DatabaseHelper(Context context, String name,
                      SQLiteDatabase.CursorFactory factory, int version) {
    super(context, MovieDataContract.DATABASE_NAME, factory, MovieDataContract.DATABASE_VERSION);

    myCR = context.getContentResolver();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(MovieDataContract.FilmEntry.SQL_CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(MovieDataContract.FilmEntry.DELETE_TABLE);
    onCreate(db);
}

public void addFilm(Film film){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(MovieDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
    values.put(MovieDataContract.FilmEntry.COLUMN_FILM_RATING,  film.getRating());
    values.put(MovieDataContract.FilmEntry.COLUMN_FILM_RUNTIME,  film.getRuntime());
    values.put(MovieDataContract.FilmEntry.COLUMN_FILM_CRITICS,  film.getCritics());
    values.put(MovieDataContract.FilmEntry.COLUMN_FILM_AUDIENCE,  film.getAudience());
    values.put(MovieDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS,  film.getSynopsis());
    values.put(MovieDataContract.FilmEntry.COLUMN_FILM_PROFILE,  film.getProfile());

    myCR.insert(FilmProvider.CONTENT_URI, values);

    db.insert(MovieDataContract.TABLE_NAME,
            null,
            values);
    db.close();
}

清单

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/TopMoviesTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DetailFragment"
        android:label="@string/title_activity_detail_fragment"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.purewowstudio.topmovies.MainActivity" />
        <provider
            android:name=".data.filmProvider"
            android:authorities="com.purewowstudio.topmovies.data.filmProvider"
            android:exported="true">
        </provider>
    </activity>
</application>

</manifest>

2 个答案:

答案 0 :(得分:13)

首先,将<provider>移至<application>的孩子,而不是<activity>

其次,将android:exported="true"更改为android:exported="false",直到您确保ContentProvider为止。按照目前的情况,如上所述修复<provider>元素位置后,任何应用都可以在您的提供商中读写任何内容,这不太可能是用户想要的内容。

答案 1 :(得分:1)

我在权限中添加了包名称。这就是我遇到此问题的原因。我必须在authorities中添加 <provider android:name="LoginContentProvider" android:authorities="com.provider.name.team" android:exported="false" />

{{1}}