gpt4 book ai didi

Android isScreenOn Nullpointer异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:01:42 26 4
gpt4 key购买 nike

我使用此代码在 Android 4.4.2 上保持屏幕打开。

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

代码确实有效,但一段时间后应用程序崩溃(可能 24 小时内发生一次)

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)

是否有解决方案或解决方法?这与硬件/软件相关吗?

我发现的唯一有用的线程是: https://code.google.com/p/android/issues/detail?id=43008

最佳答案

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 应该在 setContentView(R.layout.activity_main); 之前调用。

正确的顺序应该是

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

关于Android isScreenOn Nullpointer异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38097487/

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