gpt4 book ai didi

android - VerifyError 即使在检查了当前的 Android API 之后

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:06 26 4
gpt4 key购买 nike

我有以下 TestActivity:

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);
}
}

此代码在 Lollipop 设备上运行良好,但在较旧的 Android API 上,甚至在创建 Application 之前我都会遇到以下异常:

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)

我知道 RippleDrawable 在 API < 21 上不可用,但条件 Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP 不应该解决这个问题吗?

最佳答案

我自己修好了。 Java 不喜欢带有返回类的方法签名,在本例中是 RippleDrawable,它不知道。解决方案是在我的 getPressedColorRippleDrawable() 方法中简单地返回一个 Drawable:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Drawable getPressedColorRippleDrawable(int normalColor, int pressedColor) {
return new RippleDrawable(ColorStateList.valueOf(pressedColor), getColorDrawableFromColor(normalColor), getColorDrawableFromColor(normalColor));
}

关于android - VerifyError 即使在检查了当前的 Android API 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35695606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com