指定为非null的参数为null:参数投影内容提供者Kotlin

Nir*_*iya 0 android android-contentresolver android-contentprovider kotlin

我正在尝试从Kotlin文件查询内容提供者。请参见下面的代码:

var URI = Uri.parse("content://myprovider")
var nameUri = Uri.withAppendedPath(URI, "name")
cursor = contentResolver.query(nameUri, null, null, null, null)
Run Code Online (Sandbox Code Playgroud)

When I run this code, I am getting below error

Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter projection
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
    at android.content.ContentResolver.query(ContentResolver.java:802)
Run Code Online (Sandbox Code Playgroud)

Now, when I checked the query method signature in ContentResolver class, this is what it is

public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri,
        @Nullable String[] projection, @Nullable String selection,
        @Nullable String[] selectionArgs, @Nullable String sortOrder)
Run Code Online (Sandbox Code Playgroud)

As you can see, everything apart from Uri can be Nullable, so technically it should not throw this error.

Also, I tried giving projection but then it threw an error for selectionArgs.

Please help. Thanks in advance.

Note: Neither contentResolver not nameUri is null

Aks*_*r S 5

你可以试试这个吗

override fun query(uri: Uri, projection: Array<String>?, selection: String?,
               selectionArgs: Array<String>?, sortOrder: String?): Cursor? {

//ur code

return cursor
}
Run Code Online (Sandbox Code Playgroud)

参考此链接