0

初めてのコンテンツ プロバイダーを実装しています。一部のコンテンツを取得しようとすると、行き詰まります。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

手伝ってくれてありがとう。

4

1 に答える 1

0

Ok。私は自分自身に答えました。コンテンツを取得するコードは次のようになります。

 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 の 3 番目のパラメーターが何であるかを理解していなかったことです。

于 2013-03-19T10:22:54.573 に答える