Call requires API level 21 (Current min is 10)" when using getDrawable()

Asked

Viewed 740 times

2

When I try to compare the Drawable of a Imagebutton with a certain Drawable that mistake happens "Call requires API level 21 (Current min is 10)".

I’m using the getDrawable().getConstantState() to catch the Drawable of Imagebutton and giving a .equals(getDrawable(R.drawable.img)) in what I want to compare.

I tried to use getResources().getDrawable(R.drawable.img).getConstantState() but gave as getDrawable(int id) is deprecated

My Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.app.gustavo.meuapp"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
}

Logcat when compiling from these errors:

05-23 11:57:07.084  12236-12236/com.app.gustavo.meuapp E/﹕ Device driver API match
Device driver API version: 23
User space API version: 23

and execution upon reaching the line of

imgButton.getDrawable().equals(
                        getResources().getDrawable(R.drawable.img))

gives these:

05-23 11:57:35.219  12236-12236/com.app.gustavo.meuapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3838)
            at android.view.View.performClick(View.java:4475)
            at android.view.View$PerformClick.run(View.java:18786)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3833)
            at android.view.View.performClick(View.java:4475)
            at android.view.View$PerformClick.run(View.java:18786)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.app.gustavo.meuapp.VsDroid.jogadaDroid(VsDroid.java:54)
            at com.app.gustavo.meuapp.VsDroid.clickQuadrado(VsDroid.java:40)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at android.view.View$1.onClick(View.java:3833)
            at android.view.View.performClick(View.java:4475)
            at android.view.View$PerformClick.run(View.java:18786)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)

1 answer

2

Your application is with the configuration for Android since 2.3.3. For this there are two alternatives:

1) Modify in Gradle the minimum API to 21, attribute minSdkVersion

 defaultConfig {
        applicationId "com.example.app
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

2) According to the documentation [http://developer.android.com/reference/android/widget/ImageView.html#getDrawable%28%29] getDrawable() has been entered into API 1, are you using it? imageButton.getDrawable?

2.1) To catch a drawable from Resource just do so:

Drawable drawable = getResources().getDrawable( R.drawable.image );

EDIT:

You are using the wrong support v7, the 22 is for API 21. In Dependence uses so:

compile 'com.android.support:appcompat-v7:20.0.0'

From what I understand you want to check if a button has the right image, is that it? That’s not the best way to do it, because carrying drawables in your memory just to buy isn’t performative. I suggest that you save your Resource id and check for it. Example:

private imagemAtual = R.drawable.imagem1;

private void trocarImagem(int imageResource){
    imagemAtual = imageResource;
    imageButton.setImageResource(imageResource);
}

When you want to know what image is :

if(imagemAtual == R.drawable.imagemParaComparar)
  • Discussion moved to the chat.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.