我有以下几点:
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Button button = new Button(this);
button.setBackground(getPressedColorRippleDrawable(Color.RED, Color.BLACK));
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static RippleDrawable getPressedColorRippleDrawable(int normalColor, int pressedColor) {
return new RippleDrawable(ColorStateList.valueOf(pressedColor), getColorDrawableFromColor(normalColor), getColorDrawableFromColor(normalColor));
}
public static ColorDrawable getColorDrawableFromColor(int color) {
return new ColorDrawable(color);
}
}
此代码在棒棒糖设备上运行良好,但在较旧的android api上,甚至在创建
TestActivity
之前,我都会收到以下异常:E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.VerifyError: TestActivity
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1215)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
我知道
Application
在apisRippleDrawable不应该解决这个问题吗? 最佳答案
我自己修好的。Java不喜欢带有返回类的方法签名,在这种情况下,RippleDrawable
,它不知道。解决方法是在myDrawable
方法中简单地返回agetPressedColorRippleDrawable()
:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Drawable getPressedColorRippleDrawable(int normalColor, int pressedColor) {
return new RippleDrawable(ColorStateList.valueOf(pressedColor), getColorDrawableFromColor(normalColor), getColorDrawableFromColor(normalColor));
}