2017-07-12 7 views
0

Anstelle der direkten Abfrage von db im mit contentResolver wird null zurückgegeben. Und ich weiß nicht, muss ich etwas Code hinzufügen, oder es ist ein Fehler in meinem.contentResolver gibt null zurück

private void displayDatabaseInfo() { 
    // Define a projection that specifies which columns from the database 
    // you will actually use after this query. 
    String[] projection = { 
      PetEntry._ID, 
      PetEntry.COLUMN_PET_NAME, 
      PetEntry.COLUMN_PET_BREED, 
      PetEntry.COLUMN_PET_GENDER, 
      PetEntry.COLUMN_PET_WEIGHT }; 

    // Perform a query on the provider using the ContentResolver. 
    // Use the {@link PetEntry#CONTENT_URI} to access the pet data. 
    Cursor cursor = getContentResolver().query(
      PetEntry.CONTENT_URI, // The content URI of the words table 
      projection,    // The columns to return for each row 
      null,     // Selection criteria 
      null,     // Selection criteria 
      null);     // The sort order for the returned rows 

    Log.v(LOG_TAG, "Проверка курсора " + cursor); 



    TextView displayView = (TextView) findViewById(R.id.text_view_pet); 

    if (cursor != null){ 
    try { 


      // Create a header in the Text View that looks like this: 
      // 
      // The pets table contains <number of rows in Cursor> pets. 
      // _id - name - breed - gender - weight 
      // 
      // In the while loop below, iterate through the rows of the cursor and display 
      // the information from each column in this order. 
      displayView.setText("The pets table contains " + cursor.getCount() + " pets.\n\n"); 
      displayView.append(PetEntry._ID + " - " + 
        PetEntry.COLUMN_PET_NAME + " - " + 
        PetEntry.COLUMN_PET_BREED + " - " + 
        PetEntry.COLUMN_PET_GENDER + " - " + 
        PetEntry.COLUMN_PET_WEIGHT + "\n"); 

      // Figure out the index of each column 
      int idColumnIndex = cursor.getColumnIndex(PetEntry._ID); 
      int nameColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME); 
      int breedColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED); 
      int genderColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER); 
      int weightColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT); 

      // Iterate through all the returned rows in the cursor 
      while (cursor.moveToNext()) { 
       // Use that index to extract the String or Int value of the word 
       // at the current row the cursor is on. 
       int currentID = cursor.getInt(idColumnIndex); 
       String currentName = cursor.getString(nameColumnIndex); 
       String currentBreed = cursor.getString(breedColumnIndex); 
       int currentGender = cursor.getInt(genderColumnIndex); 
       int currentWeight = cursor.getInt(weightColumnIndex); 
       // Display the values from each column of the current row in the cursor in the TextView 
       displayView.append(("\n" + currentID + " - " + 
         currentName + " - " + 
         currentBreed + " - " + 
         currentGender + " - " + 
         currentWeight)); 
      } 

    } finally { 
     // Always close the cursor when you're done reading from it. This releases all its 
     // resources and makes it invalid. 
     cursor.close(); 
    } 
    } 
} 

Davor hatte ich einen Fehler Null Ausnahme. Nach dem Hinzufügen von if/else-Anweisung Ausnahmen verschwunden aber trotzdem funktioniert es nicht und gibt null

+0

Gibt getContentResolver() null zurück oder ist getContentResolver(). Query gibt einen Null-Cursor zurück? –

+0

@MichaelKrause Ich denke Abfrage Null zurückgeben –

+0

Sind Sie sicher, dass PetEntry.CONTENT_URI gültig ist und korrekt zu Ihrem Inhaltsanbieter zugeordnet? –

Antwort

0

Problem war in ContentProvider. Ich habe den Code geändert und jetzt funktioniert es.

@Override 
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 
        String sortOrder) { 
    // Get readable database 
    SQLiteDatabase database = mDbHelper.getReadableDatabase(); 

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

    // Figure out if the URI matcher can match the URI to a specific code 
    int match = sUriMatcher.match(uri); 
    switch (match) { 
     case PETS: 
      // For the PETS code, query the pets table directly with the given 
      // projection, selection, selection arguments, and sort order. The cursor 
      // could contain multiple rows of the pets table. 
      cursor = database.query(PetEntry.TABLE_NAME, projection, selection, selectionArgs, 
        null, null, sortOrder); 
      break; 
     case PET_ID: 
      // For the PET_ID code, extract out the ID from the URI. 
      // For an example URI such as "content://com.example.android.pets/pets/3", 
      // the selection will be "_id=?" and the selection argument will be a 
      // String array containing the actual ID of 3 in this case. 
      // 
      // For every "?" in the selection, we need to have an element in the selection 
      // arguments that will fill in the "?". Since we have 1 question mark in the 
      // selection, we have 1 String in the selection arguments' String array. 
      selection = PetEntry._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 = database.query(PetEntry.TABLE_NAME, projection, selection, selectionArgs, 
        null, null, sortOrder); 
      break; 
     default: 
      throw new IllegalArgumentException("Cannot query unknown URI " + uri); 
    } 
    return cursor; 
}