内容提供商未知URL java.lang.IllegalArgumentException错误

时间:2015-07-06 17:35:59

标签: java android

与此斗争了好几个小时,并在代码中尝试了一大堆排列。 当我尝试插入新记录时,我不断收到IllegalArgumentException。

这是宣言:

<provider android:name="com.hakeem.model.BoxerProvider" android:exported="false"
        android:readPermission="true" android:writePermission="true"
        android:authorities="com.hakeem.model" />

以下是我的合同类的相关部分:

public static final String TABLE_NAME = "boxers";   

//Column Names
public static final String _ID  = BaseColumns._ID;
public static final String BOXER_NAME = "boxer_name";
public static final String WEIGHT_CLASS = "weight_class";
public static final String WINS = "wins";
public static final String LOSSES ="losses";    

//Uri Segments
public static final String AUTHORITY = "com.hakeem.model";
public static final String PATH = TABLE_NAME;
public static final Uri BASE_URI = Uri.parse("Content://" + AUTHORITY); 
public static final Uri CONTENT_URI = BASE_URI.buildUpon().appendPath(PATH).build();    

//Integer tokens for URI Matcher
public static final int BOXER = 10;
public static final int BOXERS = 20;

//Strings differents Uri paths to identify types
public static final String BOXERS_PATH = "boxers";
public static final String BOXER_PATH_ID = "boxers/#";  

public static final String BOXER_TYPE = "vnd.android.cursor.dir/com.hakeem.model.boxers";
public static final String BOXER_ID_TYPE = "vnd.android.cursor.item/com.hakeem.model.boxers";

//Call function to build uri matcher
public static final UriMatcher URI_MATCHER = buildUriMatcher();

这是来自Provider Class的getType和insert函数

@Override
public String getType(Uri uri) {

    int uriType = BoxerContract.URI_MATCHER.match(uri);
    String sUriType;

    switch(uriType){

    case BoxerContract.BOXER:
        sUriType = BoxerContract.BOXER_ID_TYPE;
        break;
    case BoxerContract.BOXERS:
        sUriType = BoxerContract.BOXER_TYPE;
        break;
    default:
        throw new IllegalArgumentException("Unknown uri: getType" + uri);
    }

    return sUriType;
}

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

    long id;
    int uriType = BoxerContract.URI_MATCHER.match(uri);
    Uri insertedUri;
    SQLiteDatabase boxerDB;

    switch(uriType){

    case BoxerContract.BOXERS:
        boxerDB = dbHelper.getWritableDatabase();
        id = boxerDB.insert(BoxerContract.TABLE_NAME, null, values);
        break;
    default:
        throw new IllegalArgumentException("Unknown Uri: in insert statement" + uri);
    }

    insertedUri = Uri.parse(BoxerContract.TABLE_NAME + "/" + id);

    return insertedUri;
}

最后,我在我的Activity中调用insert:

values.put(BoxerContract.BOXER_NAME, edBoxerName.getText().toString());
                values.put(BoxerContract.WEIGHT_CLASS, edWeight.getText().toString());
                values.put(BoxerContract.WINS, edWins.getText().toString());
                values.put(BoxerContract.LOSSES,edLoss.getText().toString());


                getContentResolver().insert(BoxerContract.CONTENT_URI, values);

现在我的活动onCreate中的这段代码不会导致错误,但似乎被忽略了,因为我在提供程序的查询方法中的日志语句没有出现在logcat中。

cursor = getContentResolver().query(BoxerContract.CONTENT_URI, projection, null, null, null);

为简洁起见,我排除了一些代码。如果我需要发布任何其他内容,请告诉我。

1 个答案:

答案 0 :(得分:1)

将来,请发布完整的堆栈跟踪。

话虽如此,请尝试将Uri.parse("Content://" + AUTHORITY);替换为Uri.parse("content://" + AUTHORITY);,因为Android通常区分大小写。