Stefan Maimon Stefan Maimon - 1 month ago 29
Android Question

Android - setStatusBarColor() NoSuchMethodError exception

I published my Android app on Google Play and I get many reports about crashes from some devices. At first I thought that this is a SDK version problem, but after I update, it still happens. Here is the report log that I get:

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)


This is line 81 at
MainActivity class
:

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
}


It usually crash in devices such as: Galaxy S5 (kltecan),Galaxy S2 Plus (s2vep),alps..

Answer

It needs to be

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

instead of

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

as setStatusBarColor() was added in API level 21.