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

2

When I try to compare the Drawable of an ImageButton with a certain Drawable this error occurs min is 10) ".

I'm using getDrawable().getConstantState() to get the Drawable from ImageButton and giving .equals(getDrawable(R.drawable.img)) to what I want to compare.

I tried to use getResources().getDrawable(R.drawable.img).getConstantState() but gave 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 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 on the run on the line of

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

Give 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)
    
asked by anonymous 21.05.2015 / 20:47

1 answer

2

Your application has been setting up for Android since 2.3.3. For this there are two alternatives:

1) Modify in the gradle the minimum API for 21, attribute minSdkVersion

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

2) According to the link documentation or getDrawable () was inserted in API 1, are you using it like this? imageButton.getDrawable?

2.1) To get a drawable of the resource, just do the following:

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

EDIT:

You are using the wrong v7 support, 22 is for API 21. In dependence it uses like this:

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? This is not the best way to do this, because loading drawables into memory just to buy is not performative. I suggest you save the id of the resource 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 it is:

if(imagemAtual == R.drawable.imagemParaComparar)
    
21.05.2015 / 21:23