NoSuchMethodError when setting a custom progress bar

1.3k views Asked by At

I am using a custom progress bar and it works on my physical phone just fine. However, I am created a tablet layout of my app and tried it on an emulator and it gives me this error message - NoSuchMethodError
Here is the piece of code where I am setting my custom progress bar: line 34 is where I am setting the interterminateDrawable. The minimum sdks version is 14

mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    mProgressBar.setVisibility(View.INVISIBLE);
    mProgressBar.setIndeterminateDrawable(getDrawable(R.drawable.progress));

Here is the log:

java.lang.NoSuchMethodError: koemdzhiev.com.blinkmessage.LoginActivity.getDrawable
        at koemdzhiev.com.blinkmessage.LoginActivity.onCreate(LoginActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
2

There are 2 answers

2
CommonsWare On BEST ANSWER

The getDrawable() convenience method on Context was added in API Level 21. Your tablet may be running an older version.

The minimum sdks version is 14

The build tools should have complained about your use of getDrawable() here. Either raise your minSdkVersion to 21, or use something else, such as the getDrawable() method on Resources (and you can get a Resources by calling getResources() on your activity).

0
yash786 On
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        seekBar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(main_activity, R.color.your_color)));
    }
    else
    {
        seekBar.getProgressDrawable().setColorFilter(main_activity.getResources().getColor(R.color.your_color), PorterDuff.Mode.MULTIPLY);
    }