本帖最后由 陈莹 于 2012-12-12 00:02 编辑
我的程序一直报错Failed to find provider info for com.sharpandroid.provider.personprovider,就是找不到provider
谁能帮我看一下程序错在哪?
有一个PersonProvider,用于提供person表中数据的共享- package com.sharpandroid.activity;
- import android.content.ContentProvider;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.UriMatcher;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.net.Uri;
- import com.sharpandroid.activity.dao.DBHelper;
- public class PersonProvider extends ContentProvider {
- private DBHelper dbHelper;
- //路径的识别器 UriMatcher
- private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);//UriMatcher.NO_MATCH表示不匹配的话返回的码 -1
- private static final int PERSONS = 1;
- private static final int PERSON = 2;
- static{
- //为UriMatcher对象添加需要匹配的路径
- matcher.addURI("com.sharpandroid.provider.personprovider", "person", PERSONS);
- matcher.addURI("com.sharpandroid.provider.personprovider", "person/#", PERSON);
- }
-
-
- //onCreate方法在ContentProvider创建后就会被调用
- @Override
- public boolean onCreate() {
- //实例化dbHelper
- dbHelper = new DBHelper(this.getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- SQLiteDatabase db = dbHelper.getReadableDatabase();
- switch(matcher.match(uri)){
- case PERSONS:
- //查询数据库中所有的数据
- return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
- case PERSON:
- //查询数据库中特定的数据
- //从Uri中提取id
- long id = ContentUris.parseId(uri);
- //创建查询条件
- String whereClause = "personid="+id;
- if(selection!=null&&!"".equals(selection.trim())){
- whereClause += "and"+selection;
- }
- return db.query("person", projection, whereClause, selectionArgs, null, null, null);
- default:
- throw new IllegalArgumentException("Unknow Uri"+uri);
- }
- }
- @Override
- public String getType(Uri uri) {
- return null;
- }
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- return null;
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- return 0;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection,
- String[] selectionArgs) {
- return 0;
- }
- }
复制代码 然后在配置文件中已经配置了该provider- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.sharpandroid.activity"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="10" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <uses-library android:name="android.test.runner"/>
- <activity android:name=".PersonActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <!-- 配置ContentProvider -->
- <provider
- android:name=".PersonProvider"
- android:authorities="com.sharpandroid.provider.personprovider"/>
-
- </application>
- <instrumentation
- android:name="android.test.InstrumentationTestRunner"
- android:targetPackage="com.sharpandroid.activity"
- android:label="Tests for My App">
- </instrumentation>
- </manifest>
复制代码 然后我又创建了一个应用程序,通过测试代码访问该provider- package com.example.other;
- import android.content.ContentResolver;
- import android.database.Cursor;
- import android.net.Uri;
- import android.test.AndroidTestCase;
- import android.util.Log;
- /**
- * AccessContentProvider可以对PersonProvider中的数据进行操作时,
- * 可以使用ContentResolver类完成
- * @author Administrator
- *
- */
- public class AccessContentProvider extends AndroidTestCase {
- private static final String TAG = "AccessContentProvider";
- public void testQuery()throws Exception{
- //获取ContentResolver对象
- ContentResolver resolver = this.getContext().getContentResolver();
- //定义Uri,查询所有的记录
- Uri uri = Uri.parse("content://com.sharpandroid.provider.personprovider/person");
- Cursor cursor = resolver.query(uri, null, null, null, null);
- while(cursor.moveToNext()){
- int id = cursor.getInt(cursor.getColumnIndex("personid"));
- String name = cursor.getString(cursor.getColumnIndex("name"));
- int age = cursor.getInt(cursor.getColumnIndex("age"));
- //显示查询结果
- Log.i(TAG, id+":"+name+":"+age);
- }
- //关闭游标
- cursor.close();
- }
- }
复制代码 单元测试并不通过,出现上面所说的异常。那位大侠能帮我看看啊?感激不尽!!
|
|