Please, help me find a mistake! I created a Content Provider and try to read data from my database. But I get
Failed to find provider info for com.example.ifixer.data.IfixerContentProvider.
1)I described the provider in Android Manifest Android 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)I created constants in the contract 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";
}
}
- My content Provider
IfixerContentProvider.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;
}
}
- And a fragment of a Main Activity, where I trying to read a data: MainActivity.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();
}
Please, help me understand, what am I doing wrong?
CodePudding user response:
authorities
should point straight on file, so
android:authorities="com.example.ifixer.data.IfixerContentProvider"
without package
prefix