2017-07-31 20 views
0

我有一個活動類,它使用ContentResolver的query()方法,其中Uri & projection[]被設置爲其兩個參數,其餘參數設置爲'null'。 喜歡:使用內容解析器和內容提供程序時,應用程序崩潰

ContentResolver resolverCatalog = getContentResolver(); Cursor cursor = resolverCatalog.query(PetsEntry.CONTENT_URI,projection,null,null,null);

然而在ContentProvider類的query()方法被定義爲:

@Override 
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 
    // Making an instance of the SQLiteOpenHelper class named as 'SQLdbHelper' 
    SQLdbHelper PetdbHelper = new SQLdbHelper(getContext()); 

    //getting access to database 
    SQLiteDatabase database_query = PetdbHelper.getReadableDatabase(); 

    // This cursor will hold the result of the query 
    Cursor cursor_query; 

    // Figure out if the URI matcher can match the URI to a specific code 
    int match = sUriMatcher.match(uri); 
    switch (match) { 
     case PETS: 
      cursor_query = database_query.query(TABLE_NAME,projection,null,null,null,null,null); 
      break; 

     case PET_ID: 
      selection = PetContract.PetsEntry._ID + "=?"; 
      selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) }; 

      // This will perform a query on the pets table where the _id equals 3 to return a 
      // Cursor containing that row of the table. 
      cursor_query = database_query.query(TABLE_NAME, projection, selection, selectionArgs, 
        null, null, sortOrder); 
      break; 
     default: 
      throw new IllegalArgumentException("Cannot query unknown URI " + uri); 
    } 

    return cursor_query; 
} 

的PETS & PETS_ID定義(這ContentProvider類內)爲:

public class PetProvider extends ContentProvider { 

//object made of the helper class for the provider, to get access of the database 
private SQLdbHelper PetdbHelper; 


private static final int PETS = 1; 
private static final int PET_ID = 2; 

private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 

static { 
    sUriMatcher.addURI(CONTENT_AUTHORITY, PATH_PETS, PETS); 
    sUriMatcher.addURI(CONTENT_AUTHORITY, PATH_PETS_ID, PET_ID); 
}...//class continues 

AndroidManifest我寫道:

<provider 
     android:name=".data.PetProvider" 
     android:authorities="com.example.android.petsretry.data" 
     android:exported="false"> 
</provider> 

已經嘗試了很多東西,但無法擺脫它...請幫助! 在此先感謝...

+0

您應該在運行應用程序崩潰時發佈堆棧跟蹤。 – bennerv

回答

0

已經找到了解決辦法,問題是隨着Android清單文件,正確的代碼是:

<provider android:name=".data.PetProvider" android:authorities="com.example.android.petsretry.data" android:exported="false"/> 

所有「名」,「當局」和「出口」屬性應該有已經在提供商的開放標籤內,而不是在提供商的開始標籤&之間 蹄...

相關問題