8

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..

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Stefan Maimon
  • 233
  • 3
  • 12
  • this may help you http://stackoverflow.com/questions/35186/how-do-i-fix-a-nosuchmethoderror – Sree Jun 08 '15 at 09:51
  • 2
    The documentation says that the method `setStatusBarColor` is Added in API level 21. [Look here](http://developer.android.com/reference/android/view/Window.html#setStatusBarColor(int)). Put `android.os.Build.VERSION.SDK_INT >= 21` – GVillani82 Jun 08 '15 at 09:54
  • Hey @Joseph82, are you sure that the kind of devices that I`m talking about running SDK`s lower then 21? (one of them is Galaxy S5), I don`t have a way to check it because I don`t have device like that to test on. – Stefan Maimon Jun 08 '15 at 10:03
  • Galaxy S5 was originally released with API 19 (some will have been upgraded to 21). You don't need the device to test, just create a AVD emulator image with API 19 and you should reproduce the same issue. – BrentM Jun 08 '15 at 10:41
  • OK @Joseph82 and BrentM, that's really helpful! – Stefan Maimon Jun 08 '15 at 12:12

2 Answers2

23

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.

Edit : u can use like it :

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          //your code
        }
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
0

use this:

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"));  }
Md Tariqul Islam
  • 2,736
  • 1
  • 20
  • 35