android - 无法使用ContentResolver查询方法,NoSuchMethodError

标签 android gradle build.gradle android-studio-3.0 android-contentresolver

我正在创建一个应用程序来读取您手机上的电话联系人。但是,由于我不知道的原因,由于我使用的 api 版本,我在使用 query 方法时遇到了问题。它表示调用需要 API 级别 26(当前最小值为 16):android.content.ContentResolver#query

这是我在 oncreate 方法中使用的代码。

public void searchContacts() {
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null);

    if (cursor.moveToFirst()) {
        do {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            Log.i("try", name + " = " + number);
        } while (cursor.moveToNext());
    }



    cursor.close();
}

我知道它应该可以工作,因为我一直在观看的教程的 API 版本比我的低。

这是我的 gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "raymundo.elsa.elsa"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'gr.pantrif:easy-android-splash-screen:0.0.1'
    implementation 'com.android.support:gridlayout-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    compile 'com.liuguangqiang.cookie:library:0.1'
}

这是我的 logcat 中的错误。

E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: raymundo.elsa.elsa, PID: 2303
                      java.lang.NoSuchMethodError: android.content.ContentResolver.query
                          at raymundo.elsa.elsa.FillUpEmergencyContacts.searchContacts(FillUpEmergencyContacts.java:130)
                          at raymundo.elsa.elsa.FillUpEmergencyContacts.onCreate(FillUpEmergencyContacts.java:52)
                          at android.app.Activity.performCreate(Activity.java:5231)
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
                          at android.app.ActivityThread.access$800(ActivityThread.java:135)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:136)
                          at android.app.ActivityThread.main(ActivityThread.java:5019)
                          at java.lang.reflect.Method.invokeNative(Native Method)
                          at java.lang.reflect.Method.invoke(Method.java:515)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                          at dalvik.system.NativeStart.main(Native Method).
    Application Terminated.

最佳答案

ContentResolver 上有三种类型的 query()four-parameter flavor that you are using是 Android 8.0 的新增功能(API 级别 26)。

五参数变体自 API 级别 1 以来就已经存在,六参数变体自 API 级别 16 以来就已经存在。您可能希望切换到其中之一。

关于android - 无法使用ContentResolver查询方法,NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61649028/

相关文章:

Android Studio 模拟器 W/AudioTrack 错误

java - Firebase 身份验证 : how to get current user's Password?

gradle - "com.jfrog.artifactory-upload"发生了什么?

Gradle - 帮助为 Ivy Publish 动态设置修订

android - 应用程序 :mergeDebugResources error in Android Studio after upgrade

android-studio - 找不到 com.android.tools.build :gradle:7. 3.1

java - 为什么 Android 不实习字符串资源?

安卓 : Open google play dialog from my app

android - 错误夸大设计支持库中的任何内容

android - 在 xml 中设置变量,就像在 android 中为 adMob 设置 list 占位符一样