我用来理解内容提供者的教程
应用程序A: https ://www.tutorialspoint.com/android/android_content_providers.htm
我创建了另一个我打算使用的应用程序来访问教程应用程序的内容提供程序
AppB清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApplication2">
<uses-permission android:name="com.example.MyApplication.StudentProvider.READ_PERMISSION"/>
<uses-permission android:name="com.example.MyApplication.StudentProvider.WRITE_PERMISSION"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.permRead"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AppB部分代码:
String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
Cursor c = getContentResolver().query(uri,null,null,null,null);
String _ID = "_id";
String NAME = "name";
String GRADE = "grade";
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(_ID)) +
", " + c.getString(c.getColumnIndex(NAME)) +
", " + c.getString(c.getColumnIndex(GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
我收到以下错误:
关于我为什么会收到此错误的任何想法?任何帮助表示赞赏!
编辑:我得到了解决方案!( //此行是新的 & //行已修改)
String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
ContentResolver cr = getContentResolver(); //This line is new
Cursor c = cr.query(uri,null,null,null,null); //Line modified
String _ID = "_id";
String NAME = "name";
String GRADE = "grade";
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(_ID)) +
", " + c.getString(c.getColumnIndex(NAME)) +
", " + c.getString(c.getColumnIndex(GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
现在我的 AppB 能够从 AppA 读取 ContentProvider,但是我仍然不明白为什么这会有所作为!
EDIT2:已经用ChangedManifest XML替换了教程 Manifest