2013-05-10 39 views
0

編輯 我得到一個java.lang.IllegalArgumentException: Unsupported URI: content://com.example.locationreminder.remindercontentprovider/items不支持的URI時,內容提供商

應該不是我能夠挑選我想管理局串什麼名字?以及內容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; 
    } 

注:我還實施插入(),更新()和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

是否當開關(sUriMatcher.match(URI))下降到默認情況下,錯誤發生的呢? 如果是這樣,那麼你需要通過匹配ALLROWS或SINGLE_ROW的uri

+0

是的,但我該怎麼做?我認爲內容解析器在我調用時調用它:getLoaderManager()。initLoader(0,null,this);查詢()切換語句中的URI是內容://com.example.locationreminder.remindercontentprovider/items – user2246120 2013-05-10 04:31:30

+0

那麼爲什麼異常說內容://com.example.locationreminder.remindercontentprovider(不包括/項後綴)?看起來你沒有在創建Loader – Y2i 2013-05-10 05:56:25

+0

時附加「/ items」!我編輯了這篇文章:它將「/ items」附加到查詢()中。但是現在我得到另一個異常:IllegalArgumentException:找不到列'_id'。我從MainActivity添加了onCreate方法。查詢方法中的投影String []僅具有標題,日期字段。那從哪裏來的? – user2246120 2013-05-11 16:58:30