访问内容提供商时不受支持的uri

时间:2013-05-10 03:35:19

标签: android android-contentprovider

修改 我得到java.lang.IllegalArgumentException: Unsupported URI: content://com.example.locationreminder.remindercontentprovider/items

我不应该为AUTHORITY字符串选择我想要的名字吗?以及内容URI?

我有以下代码:

   public class ReminderContentProvider extends ContentProvider {

   private DatabaseHelper mDbHelper;
   public static final String AUTHORITY = "com.example.locationreminder.remindercontentprovider";
   public static final Uri CONTENT_URI =  Uri.parse("content://" + AUTHORITY + "/items");
   public static final String TABLE_NAME = "reminder";
   private static final int ALLROWS = 1;
   private static final int SINGLE_ROW = 2;

   public static interface ReminderColumns extends BaseColumns {
      public static final Uri CONTENT_URI = ReminderContentProvider.CONTENT_URI;
      public static final String TITLE = "Title";
      public static final String DATE = "Date";
      public static final String CONTENT_PATH = "items";
      public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.locationreminder.items";
      public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.locationreminder.items";
   }
   static {
    sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    sUriMatcher.addURI(AUTHORITY, ReminderColumns.CONTENT_PATH, ALLROWS);
    sUriMatcher.addURI(AUTHORITY, ReminderColumns.CONTENT_PATH + "/#", SINGLE_ROW);
  }

@Override
public boolean onCreate() {
    mDbHelper = new DatabaseHelper(getContext());
    return true;
}

@Override
public String getType(Uri uri) {

    switch(sUriMatcher.match(uri)) {
    case ALLROWS:
        return ReminderColumns.CONTENT_TYPE;
    case SINGLE_ROW:
        return ReminderColumns.CONTENT_ITEM_TYPE;
    default: 
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
}


@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); 
       builder.setTables(DatabaseHelper.REMINDER_TABLE_NAME); 

       switch (sUriMatcher.match(uri)) { 
          case ALLROWS: 
             // all nice and well 
             break; 
          case SINGLE_ROW: 
             // limit query to one row at most: 
             builder.appendWhere(ReminderColumns._ID + " = " + uri.getLastPathSegment()); 
             break; 
          default: 
             throw new IllegalArgumentException("Unsupported URI: " + uri); 
       } 
       Cursor cursor = builder.query(mDbHelper.getWritableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); 
       // if we want to be notified of any changes: 
       cursor.setNotificationUri(getContext().getContentResolver(), uri); 
       return cursor; 
     }

注意:我还实现了 insert(),update()和delete(),这些与我得到的错误无关。

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationreminder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <provider 
        android:name="com.example.locationreminder.ReminderContentProvider" 
        android:authorities="com.example.locationreminder.remindercontentprovider"
        android:singleUser="false" /> <!-- other users can't use this provider -->
    <activity
        android:name="com.example.locationreminder.MainActivity"
        android:theme="@android:style/Theme.Holo.Light"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.locationreminder.NewReminderActivity"
        android:label="@string/title_activity_new_reminder"
        android:theme="@style/CalendarTheme.WithActionBar" >
    </activity>
</application>

修改

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setTheme(android.R.style.Theme_Holo_Light);
   setContentView(R.layout.activity_main);
   Cursor cursor = managedQuery(ReminderContentProvider.CONTENT_URI, null, null, null, null);

   mSimpleCursorAdapter = new SpecialAdapter(this, 
    R.layout.row, 
            cursor,
            new String[]{ DatabaseHelper.KEY_ID, DatabaseHelper.KEY_TITLE, DatabaseHelper.KEY_DATE }
            new int[] { R.id.titleID, R.id.dateTimeOrLocationID },   
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

   getLoaderManager().initLoader(0, null, this);
   ListView listView = (ListView) findViewById(R.id.list);
   listView.setAdapter(mSimpleCursorAdapter);
}

1 个答案:

答案 0 :(得分:1)

当switch(sUriMatcher.match(uri))属于默认情况时,是否会发生错误? 如果是这样,那么你需要传递匹配ALLROWS或SINGLE_ROW

的uri