Android开发报错:Failed to find provider info 问题排查求助
问题排查与修复方案
核心错误:Manifest中Provider Authorities配置错误
你在AndroidManifest.xml里的android:authorities属性值多了package 前缀,导致系统无法匹配到正确的ContentProvider,这是触发Failed to find provider info for com.example.ifixer.data.IfixerContentProvider报错的直接原因。
错误配置:
<provider android:authorities="package com.example.ifixer.data.IfixerContentProvider" android:name=".data.IfixerContentProvider" android:enabled="true" android:exported="false" android:grantUriPermissions="true"> </provider>
修正后:
<provider android:authorities="com.example.ifixer.data.IfixerContentProvider" android:name=".data.IfixerContentProvider" android:enabled="true" android:exported="false" android:grantUriPermissions="true"> </provider>
系统通过authorities值定位ContentProvider,该值必须和IfixerContract.AUTHORITY中定义的字符串完全一致。
其他潜在问题及修复
1. Query方法中表名错误
在IfixerContentProvider.query()的TASKS分支里,你错误使用了字段名TaskTable.TASK_NAME作为表名,实际应该用TaskTable.TABLE_NAME:
错误代码:
cursor = db.query(TaskTable.TASK_NAME, projection, selection, selectionArgs, null, null, sortOrder);
修正后:
cursor = db.query(TaskTable.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
2. Cursor空指针风险
在MainActivity.displayTasks()中,判断cursor == null后未提前返回,后续直接操作cursor会触发空指针崩溃:
错误代码:
if(cursor == null){ Log.d("db_task", "Get nothing"); } // 未返回,继续操作cursor
修正后:
if(cursor == null){ Log.d("db_task", "Get nothing"); dataTextView.append("没有查询到数据"); return; // 提前返回避免空指针 }
3. 建议添加任务列表Uri常量
在IfixerContract中添加任务列表的Uri常量,避免硬编码Uri字符串:
public static final Uri TASK_CONTENT_URI = BASE_CONTENT_URI.buildUpon() .appendPath(PATH_TASKS) .build();
之后在MainActivity中直接使用:
Cursor cursor = getContentResolver().query( IfixerContract.TASK_CONTENT_URI, projection, null, null, null );
4. 未实现getType方法
ContentProvider要求必须实现getType()方法,返回对应Uri的MIME类型,否则可能影响系统对Provider的识别:
@Override public String getType(Uri uri) { final int match = uriMatcher.match(uri); switch (match) { case TASKS: return "vnd.android.cursor.dir/" + AUTHORITY + "." + PATH_TASKS; case TASK_ID: return "vnd.android.cursor.item/" + AUTHORITY + "." + PATH_TASKS; default: throw new IllegalArgumentException("Unknown URI: " + uri); } }
内容的提问来源于stack exchange,提问作者Lutin
相关产品推荐
相关产品推荐

