在编码过程中,我遇到以下异常之一:
09-12 14:12:36.327 10764-10764/cn.com.luckytry.interview E/ActivityThread: Failed to find provider info for cn.com.luckytry.interview.service
09-12 14:12:36.327 10764-10764/cn.com.luckytry.interview E/ContentResolver: query() unstableProvider is null
我正在AndroidManifest文件AndroidManifest中注册和声明权限代码,如下所示:
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name=".service.InterViewPathProvider"
android:authorities="cn.com.luckytry.interview.service"
android:permission="cn.com.service.InterViewPathProvider"
android:enabled="true"
android:exported="true"
android:process=":Provider">
</provider>
...
</application>
<permission
android:name="cn.com.service.InterViewPathProvider"
android:protectionLevel="normal" />
自定义ContentProvider:
public class InterViewPathProvider extends ContentProvider {
public static final String AUTHORITY = "cn.com.luckytry.interview.service";
public static final Uri PATH_CONTENT_URI = Uri.parse("content://"
+ AUTHORITY + "/path");
public static final int PATH_URI_CODE = 0;
private static final UriMatcher sUriMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sUriMatcher.addURI(AUTHORITY, "path", PATH_URI_CODE);
}
private Context mContext;
private SQLiteDatabase mDb;
@Override
public boolean onCreate() {
mContext = getContext();
mDb =new DbOpenHelper(mContext).getReadableDatabase();
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
String table = getTableName(uri);
if (table == null) {
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
LUtil.e("query");//日志没有输出
return mDb.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
}
...
private String getTableName(Uri uri) {
LUtil.e("getTableName");
String tableName = null;
switch (sUriMatcher.match(uri)) {
case PATH_URI_CODE:
tableName = "interpath.db";
break;
default:break;
}
return tableName;
}
}
服务使用:
/**
* 解析查询结果
* @param id
* @return
*/
private String getQueryResult(int id) {
Uri userUri = InterViewPathProvider.PATH_CONTENT_URI;
//光标cursor = db.query(&#34; news&#34;,null,&#34; commentcount&gt;?&#34;,new String [] {&#34; 0&#34;}, null,null,null); String result = null;
Cursor pathCursor = getContentResolver().query(userUri, null, "beanId=?", new String[]{id+""}, null);
if(pathCursor!=null){
while (pathCursor.moveToNext()) {
result = pathCursor.getString(1);
}
pathCursor.close();
}
return result;
}