ContentResolver query method cannot be used, NoSuchMethodError

送分小仙女□ 提交于 2020-08-18 21:02:53

问题


I am creating an app that reads the phone contacts of your phone. However, for reasons that I don't know, I have a problem using the query method because of the api version that I used. It says that Call requires API level 26 (current min is 16): android.content.ContentResolver#query.

Here's my code I used in the oncreate method.

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();
}

I know it should work since the tutorials that I have been watching has a lower API version than mine.

Here's my gradle file:

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'
}

Here's the error in my 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.

回答1:


There are three flavors of query() on ContentResolver. The four-parameter flavor that you are using is new to Android 8.0 (API Level 26).

The five-parameter variant has been around since API Level 1, and the six-parameter variant has been around since API Level 16. You may wish to switch to one of those.



来源:https://stackoverflow.com/questions/48851500/contentresolver-query-method-cannot-be-used-nosuchmethoderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!