Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java contentResolver返回null_Java_Android_Sqlite - Fatal编程技术网

Java contentResolver返回null

Java contentResolver返回null,java,android,sqlite,Java,Android,Sqlite,不是使用contentResolver直接查询db im,而是返回null。我不知道我是否需要添加一些代码,或者我的代码有错误 private void displayDatabaseInfo() { // Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = {

不是使用contentResolver直接查询db im,而是返回null。我不知道我是否需要添加一些代码,或者我的代码有错误

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();
    }
    }
}
private void displayDatabaseInfo(){
//定义一个投影,指定数据库中的哪些列
//您将在该查询之后实际使用。
字符串[]投影={
彼得里,
PetEntry.COLUMN\u PET\u NAME,
彼得特里,你的宠物品种,
PetEntry.COLUMN\u PET\u性别,
PetEntry.COLUMN_PET_WEIGHT};
//使用ContentResolver对提供程序执行查询。
//使用{@link PetEntry#CONTENT_URI}访问宠物数据。
Cursor Cursor=getContentResolver().query(
PetEntry.CONTENT\u URI,//单词表的内容URI
投影,//每行要返回的列
null,//选择条件
null,//选择条件
null);//返回行的排序顺序
Log.v(Log_标签,“ППааааааааааааа107;
TextView displayView=(TextView)findViewById(R.id.text\u view\u pet);
如果(光标!=null){
试一试{
//在文本视图中创建如下所示的标题:
//
//宠物表包含宠物。
//_id-姓名-品种-性别-体重
//
//在下面的while循环中,遍历光标的行并显示
//按此顺序显示每列中的信息。
displayView.setText(“pets表包含”+cursor.getCount()+“pets。\n\n”);
displayView.append(PetEntry.\u ID+“-”+
PetEntry.COLUMN\u PET\u NAME+“-”+
PetEntry.COLUMN_宠物品种+“-”+
PetEntry.COLUMN_宠物_性别+“-”+
PetEntry.COLUMN_PET_WEIGHT+“\n”);
//计算出每列的索引
int-idColumnIndex=cursor.getColumnIndex(PetEntry.\u-ID);
int nameColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN\u PET\u NAME);
int breedColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN\u PET\u BREED);
int genderColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN\u PET\u GENDER);
int-weightColumnIndex=cursor.getColumnIndex(PetEntry.COLUMN\u PET\u WEIGHT);
//遍历游标中返回的所有行
while(cursor.moveToNext()){
//使用该索引提取单词的字符串或Int值
//光标位于当前行上。
int currentID=cursor.getInt(idColumnIndex);
String currentName=cursor.getString(nameColumnIndex);
字符串currentBreed=cursor.getString(breedColumnIndex);
int currentGender=cursor.getInt(genderColumnIndex);
int currentWeight=cursor.getInt(weightColumnIndex);
//在TextView中的光标中显示当前行的每列的值
displayView.append((“\n”+currentID+“-”+
currentName+“-”+
当前品种+“-”+
当前性别+“-”+
当前权重);
}
}最后{
//读取完光标后,请始终关闭光标。这将释放所有光标
//并使其无效。
cursor.close();
}
}
}

在此之前,我得到了一个错误null异常。添加if/else语句后,异常消失,但无论如何它都不工作并返回null

问题出在ContentProvider中。我已经更改了代码,现在它可以工作了

  @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;
}

问题出在ContentProvider中。我已经更改了代码,现在它可以工作了

  @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;
}

getContentResolver()是否返回null或getContentResolver()是否返回null.query返回空光标?@MichaelKrause我猜query返回空你确定PetEntry.CONTENT\u URI有效并正确映射到你的内容提供商吗?@MichaelKrause是的,因为在使用contentResolver之前,我直接向数据库查询,一切都很好。这是使用基于从数据库获取的PetEntry.CONTENT\u URI的ContentProviderClient我接受acquireContentProviderClient?getContentResolver()是否返回null或getContentResolver()是否为空.query返回空光标?@MichaelKrause我猜query返回空你确定PetEntry.CONTENT\u URI有效并正确映射到你的内容提供商吗?@MichaelKrause是的,因为在使用contentResolver之前,我直接向数据库查询,一切都很好。这是使用基于从数据库获取的PetEntry.CONTENT\u URI的ContentProviderClientacquireContentProviderClient我接受了吗?