Android - setStatusBarColor()NoSuchMethodError异常

时间:2015-06-08 09:48:40

标签: android android-activity android-support-library android-version

我在Google Play上发布了我的Android应用,我收到了很多关于某些设备崩溃的报告。起初我认为这是一个SDK版本问题,但在我更新后,它仍然会发生。这是我得到的报告日志:

java.lang.NoSuchMethodError: android.view.Window.setStatusBarColor
at stefanmaimon.milab.blenders.MainActivity.onCreate(MainActivity.java:81)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)

这是MainActivity class的第81行:

if (android.os.Build.VERSION.SDK_INT >= 19) {
    Window statusBar = getWindow();
    statusBar.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    statusBar.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    statusBar.setStatusBarColor(Color.parseColor("#111111")); <--- LINE 81
}

它通常会在以下设备中崩溃:Galaxy S5(kltecan),Galaxy S2 Plus(s2vep),阿尔卑斯山......

2 个答案:

答案 0 :(得分:20)

需要

if (android.os.Build.VERSION.SDK_INT >= 21)

而不是

if (android.os.Build.VERSION.SDK_INT >= 19)

在API级别21中添加了setStatusBarColor()

答案 1 :(得分:0)

使用它:

if (android.os.Build.VERSION.SDK_INT >= 21) {
    Window statusBar = getWindow();
    statusBar.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    statusBar.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    statusBar.setStatusBarColor(Color.parseColor("#111111"));  }