请帮助我查找错误!我创建了一个内容提供程序,并尝试从数据库读取数据。但我得到
找不到com. example. ifixer. data. ifixerContentProvider的提供程序信息。
1)我在Android ManifestAndroid Manifest.xml中描述了提供程序:
<provider
android:authorities="package com.example.ifixer.data.IfixerContentProvider"
android:name=".data.IfixerContentProvider"
android:enabled="true"
android:exported="false"
android:grantUriPermissions="true">
</provider>
2)我在合约中创建了常量IfixerContract.java:
package com.example.ifixer.data;
import android.net.Uri;
import android.provider.BaseColumns;
import java.net.URI;
final public class IfixerContract {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "ifixer";
public static final String SCHEME = "content://";
public static final String AUTHORITY = "com.example.ifixer.data.IfixerContentProvider";
public static final String PATH_TASKS = "tasks";
public static final Uri BASE_CONTENT_URI = Uri.parse(SCHEME+AUTHORITY);
private IfixerContract(){
}
public static final class TaskTable implements BaseColumns{
public static final String TABLE_NAME = "tasks";
public static final String KEY_ID = "id";
public static final String TASK_NAME = "task_name";
public static final String BEEP_RULE = "beep_rule";
}
}
1.我的内容提供程序
标识符内容提供程序.java:
public class IfixerContentProvider extends ContentProvider {
DBHandler dbOpenHelper;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int TASKS = 111;
private static final int TASK_ID = 221;
static{
uriMatcher.addURI(IfixerContract.AUTHORITY, IfixerContract.PATH_TASKS, TASKS);
uriMatcher.addURI(IfixerContract.AUTHORITY, IfixerContract.PATH_TASKS+"/#", TASK_ID);
}
@Override
public boolean onCreate() {
dbOpenHelper = new DBHandler(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder){
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor;
int match = uriMatcher.match(uri);
switch(match){
case TASKS:{
Log.d("db_task", "I'm in TASKS");
cursor = db.query(TaskTable.TASK_NAME,
projection, selection, selectionArgs,
null, null, sortOrder);
break;
}
case TASK_ID:{
Log.d("db_task", "I'm in TASK_ID");
selection = TaskTable.KEY_ID+"=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
cursor = db.query(TaskTable.TABLE_NAME,
projection, selection, selectionArgs,
null, null, sortOrder);
break;
}
default:{
Log.d("db_task", "Incorrect URI");
throw new IllegalArgumentException("Can't query incorrect URI "+uri);
}
}
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor;
int match = uriMatcher.match(uri);
switch(match){
case TASKS:{
long id = db.insert(TaskTable.TABLE_NAME,
null,
contentValues);
if(id == -1) {
Log.d("db_uri", "Insertion failed for " + uri);
return null;
}
return ContentUris.withAppendedId(uri, id);
}
default:{
Log.d("db_uri", "Insertion failed for");
return null;
}
}
}
@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
}
1.还有一个Main Activity的片段,我尝试在其中读取数据:主要活动.java:
@Override
protected void onStart() {
super.onStart();
displayTasks();
}
private void displayTasks(){
String[] projection = {
TaskTable.KEY_ID,
TaskTable.TASK_NAME,
TaskTable.BEEP_RULE
};
Cursor cursor = getContentResolver().query(
Uri.parse("content://com.example.ifixer.data.IfixerContentProvider/tasks"), //IfixerContract.TASK_CONTENT_URI,
projection,
null,
null,
null
);
if(cursor == null){
Log.d("db_task", "Get nothing");
}
dataTextView.setText("Все задачи \n\n");
dataTextView.append(TaskTable.KEY_ID
+" "+TaskTable.TASK_NAME
+" "+TaskTable.BEEP_RULE);
int idIndex = cursor.getColumnIndex(TaskTable.KEY_ID);
int idTaskName = cursor.getColumnIndex(TaskTable.TASK_NAME);
int idBeepRule = cursor.getColumnIndex(TaskTable.BEEP_RULE);
while(cursor.moveToNext()){
int currentId = cursor.getInt(idIndex);
String currentTaskName = cursor.getString(idTaskName);
String currentRule = cursor.getString(idBeepRule);
dataTextView.append("\n"+String.valueOf(currentId)+" "+currentTaskName+" "+currentRule);
}
cursor.close();
}
拜托,让我明白我做错了什么?
1条答案
按热度按时间jyztefdp1#
authorities
应直接指向文件,因此不带
package
前缀