Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why getDrawable() doesn't work on some Android devices?

Tags:

I am getting "nosuchmethod error" on some user's phones (eg. Motorola Razr i) but it works fine on my HTC. Below is the code.

Drawable rBlack; rBlack = getResources().getDrawable(R.drawable.rblack, getTheme()); 

And the Stacktrace:

java.lang.NoSuchMethodError: android.content.res.Resources.getDrawable at org.petero.droidfish.myClass.onCreate(myClass.java:434) at android.app.Activity.performCreate(Activity.java:5248) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) at android.app.ActivityThread.access$800(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5102) 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:803) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) at dalvik.system.NativeStart.main(Native Method) 

And if I use this code:

flipButton.setImageDrawable(getDrawable(R.drawable.rblack)); 

I get this error

java.lang.NoSuchMethodError: org.myclass.getDrawable at org.myclass.onCreate(myclass.java:435) at android.app.Activity.performCreate(Activity.java:5248) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) at android.app.ActivityThread.access$800(ActivityThread.java:139) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5102) 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:803) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) at dalvik.system.NativeStart.main(Native Method) 
like image 892
savante Avatar asked May 15 '15 19:05

savante


1 Answers

I'm going to bet it is failing on getDrawable(R.drawable.rblack, getTheme()); which was added in API 21. Change that line to:

if(android.os.Build.VERSION.SDK_INT >= 21){     rBlack = getResources().getDrawable(R.drawable.rblack, getTheme()); } else {     rBlack = getResources().getDrawable(R.drawable.rblack); } 
like image 147
Chris Stillwell Avatar answered Oct 06 '22 01:10

Chris Stillwell