Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android isScreenOn Nullpointerexception

i used this code to keep the screen on on Android 4.4.2.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

The code does work, but after some time the app crashes (maybe one time in 24 hours)

java.lang.NullPointerException
    at android.os.PowerManager.isScreenOn(PowerManager.java:739)
    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:454)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:250)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3028)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3930)
    at android.app.ActivityThread.access$900(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5292)
    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:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)

Is there a solution or workarround for this? Is this hardware/software related?

the only useful thread i found was: https://code.google.com/p/android/issues/detail?id=43008

like image 370
Fabian Avatar asked Oct 18 '22 06:10

Fabian


1 Answers

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); should be called before setContentView(R.layout.activity_main);.

The correct sequence should be

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.activity_main);

like image 124
am110787 Avatar answered Oct 29 '22 18:10

am110787