UriMatcher与ContentProvider Android中的uri不匹配

时间:2013-03-19 09:59:30

标签: android android-contentprovider

我正在实施我的第一个内容提供商。我试图检索一些内容时遇到困难。似乎UriMatcher无法与我通过的uri相匹配。

这是MyContentProvider类:

 public class MyContentProvider extends ContentProvider {

private DataBaseHelper database;


static final String TAG = "CONTENT";
 // Used for the UriMacher
public static final int ALL = 10;


  public static final String AUTHORITY = "my.provider";

  private static final String BASE_PATH = "resources";
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
      + "/" + BASE_PATH);

  public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
      + "/all";
  public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
      + "/one";

  private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  static {
    sURIMatcher.addURI(AUTHORITY, BASE_PATH , ALL);

  }

@Override
public boolean onCreate() {
  database = new DataBaseHelper(getContext());

return true;
}
@Override
public String getType(Uri uri) {
String ret = getContext().getContentResolver().getType(CONTENT_URI);
Log.i(TAG, "getType returning: " + ret);
return ret;
}


@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
    // Uisng SQLiteQueryBuilder instead of query() method
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    // Set the table
    queryBuilder.setTables(DataBaseHelper.MYTABLE);

    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
    case ALL:
      break;

    default:
          // THIS LINE THROWS EXCEPTION!!
      throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    SQLiteDatabase db = database.getWritableDatabase();
    Cursor cursor = queryBuilder.query(db, projection, selection,
        selectionArgs, null, null, sortOrder);
    // Make sure that potential listeners are getting notified
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
}


   }

以下是我在查询MyContentProvider时使用的代码

      Uri.Builder builder = new Uri.Builder();
      builder.scheme ("content");
      builder.authority(MyContentProvider.AUTHORITY);

      //builder.path (inURI.getPath ());
      builder.path("resources");
      builder.appendPath(""+MyContentProvider.ALL);

      Uri uri = builder.build();

      Cursor cursor = getContentResolver().query(uri, null, null, null,
                null);

在这里我的logcat:

03-19 10:43:49.204: E/AndroidRuntime(12430): FATAL EXCEPTION: main
03-19 10:43:49.204: E/AndroidRuntime(12430): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.MainActivity}: java.lang.IllegalArgumentException: Unknown URI: content://falcofinder.android.memorapp/resources/10
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.os.Looper.loop(Looper.java:130)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.app.ActivityThread.main(ActivityThread.java:3835)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at java.lang.reflect.Method.invoke(Method.java:507)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at dalvik.system.NativeStart.main(Native Method)
03-19 10:43:49.204: E/AndroidRuntime(12430): Caused by: java.lang.IllegalArgumentException: Unknown URI: content://my.provider/resources/10
03-19 10:43:49.204: E/AndroidRuntime(12430):    at my.package.MyContentProvider.query(MyContentProvider.java:95)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.content.ContentProvider$Transport.query(ContentProvider.java:187)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.content.ContentResolver.query(ContentResolver.java:271)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at my.package.MainActivity.onCreate(MainActivity.java:95)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 10:43:49.204: E/AndroidRuntime(12430):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
03-19 10:43:49.204: E/AndroidRuntime(12430):    ... 11 more

谢谢,求助。

1 个答案:

答案 0 :(得分:0)

确定。我回答了自己。检索内容的代码应该是这样的。

 Uri.Builder builder = new Uri.Builder();
  builder.scheme ("content");
  builder.authority(MyContentProvider.AUTHORITY);

  //builder.path (inURI.getPath ());
  builder.path("resources");
      Uri uri = builder.build();

  Cursor cursor = getContentResolver().query(uri, null, null, null,
            null);

我的问题是我不明白addURI的第三个参数是什么。