使用内容解析器和内容提供程序时,应用程序崩溃

时间:2017-07-31 01:55:54

标签: android mysql

我有一个活动类,它使用ContentResolver的query()方法,Uri & projection[]被设置为其两个参数,其余设置为' null'。 像:

ContentResolver resolverCatalog = getContentResolver(); Cursor cursor = resolverCatalog.query(PetsEntry.CONTENT_URI,projection,null,null,null);

但是在ContentProvider类中,query()方法定义为:

 @Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    // Making an instance of the SQLiteOpenHelper class named as 'SQLdbHelper'
    SQLdbHelper PetdbHelper = new SQLdbHelper(getContext());

    //getting access to database
    SQLiteDatabase database_query = PetdbHelper.getReadableDatabase();

    // This cursor will hold the result of the query
    Cursor cursor_query;

    // Figure out if the URI matcher can match the URI to a specific code
    int match = sUriMatcher.match(uri);
    switch (match) {
        case PETS:
            cursor_query = database_query.query(TABLE_NAME,projection,null,null,null,null,null);
            break;

        case PET_ID:
            selection = PetContract.PetsEntry._ID + "=?";
            selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };

            // This will perform a query on the pets table where the _id equals 3 to return a
            // Cursor containing that row of the table.
            cursor_query = database_query.query(TABLE_NAME, projection, selection, selectionArgs,
                    null, null, sortOrder);
            break;
        default:
            throw new IllegalArgumentException("Cannot query unknown URI " + uri);
    }

    return cursor_query;
}

PETS& PETS_ID在(在此ContentProvider类中)定义为:

public class PetProvider extends ContentProvider {

//object made of the helper class for the provider, to get access of the database
private SQLdbHelper PetdbHelper;


private static final int PETS = 1;
private static final int PET_ID = 2;

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {
    sUriMatcher.addURI(CONTENT_AUTHORITY, PATH_PETS, PETS);
    sUriMatcher.addURI(CONTENT_AUTHORITY, PATH_PETS_ID, PET_ID);
}...//class continues

AndroidManifest我写道:

 <provider
        android:name=".data.PetProvider"
        android:authorities="com.example.android.petsretry.data"
        android:exported="false">
 </provider>

已尝试了多项内容,但无法摆脱它...请帮忙! 提前谢谢......

1 个答案:

答案 0 :(得分:0)

找到解决方案,问题出在Android Manifest文件中,正确的代码是:

<provider android:name=".data.PetProvider" android:authorities="com.example.android.petsretry.data" android:exported="false"/>

所有'name','authority'和'exported'属性应该在提供者的开头标记内,而不是在开头和开头之间。关闭提供者的标签...... 蹄...