本文介绍了为什么getDrawable()在某些Android设备上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在某些用户的电话(例如Motorola Razr i)上收到"nosuchmethod错误",但在HTC上工作正常.下面是代码.
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());
还有堆栈跟踪:
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)
如果我使用此代码:
flipButton.setImageDrawable(getDrawable(R.drawable.rblack));
我收到此错误
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)
推荐答案
我敢打赌它在API 21中添加的getDrawable(R.drawable.rblack, getTheme());
上失败.将该行更改为:
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);
}
这篇关于为什么getDrawable()在某些Android设备上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!