programacion - ¿Por qué getDrawable() no funciona en algunos dispositivos Android?
manual de android en pdf (3)
Intenta agregar la siguiente línea de código:
context.getResources().getDrawable(R.drawable.your_image_name);
Estoy recibiendo un "error nosuchmethod" en los teléfonos de algunos usuarios (por ejemplo, Motorola Razr i) pero funciona bien en mi HTC. A continuación se muestra el código.
Drawable rBlack;
rBlack = getResources().getDrawable(R.drawable.rblack, getTheme());
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)
Y si uso este código:
flipButton.setImageDrawable(getDrawable(R.drawable.rblack));
Me sale este 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)
Voy a apostar que está fallando en getDrawable(R.drawable.rblack, getTheme());
que se agregó en API 21. Cambie esa línea a:
if(android.os.Build.VERSION.SDK_INT >= 21){
rBlack = getResources().getDrawable(R.drawable.rblack, getTheme());
} else {
rBlack = getResources().getDrawable(R.drawable.rblack);
}
ContextCompat.getDrawable(Context context, int id)