gpt4 book ai didi

java - 尝试释放 WakeLock 时强行关闭?

转载 作者:行者123 更新时间:2023-11-29 22:05:12 27 4
gpt4 key购买 nike

我有一个按钮,当您单击它时,它会打开唤醒锁,而且效果很好!但是,当您再次单击它时,它应该会关闭 WakeLock,但它只会让应用程序崩溃!任何帮助将不胜感激,我知道这不是因为我正在更改图像,因为那部分工作正常! :) 这是我的代码:

PowerManager.WakeLock wl; //This is before my oncreate
int stayAwake = 0; //This is before my oncreate

((Button) findViewById(R.id.sleepLock)).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
PowerManager pm = (PowerManager) getSystemService (Context.POWER_SERVICE);
wl = pm.newWakeLock (PowerManager.FULL_WAKE_LOCK, "My Tag");
if (stayAwake == 0) {
((Button) findViewById(R.id.sleepLock)).setBackgroundResource(R.drawable.awake);
stayAwake = 1;
wl.acquire();
}else {
((Button) findViewById(R.id.sleepLock)).setBackgroundResource(R.drawable.sleep);
stayAwake = 0;
wl.release();
}

// TODO Auto-generated method stub

}
});

日志:

06-06 00:44:25.206: W/dalvikvm(3855): threadid=1: thread exiting with uncaught exception (group=0x40a031f8)
06-06 00:44:25.230: E/AndroidRuntime(3855): FATAL EXCEPTION: main
06-06 00:44:25.230: E/AndroidRuntime(3855): java.lang.RuntimeException: WakeLock under-locked My Tag
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.os.PowerManager$WakeLock.release(PowerManager.java:325)
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.os.PowerManager$WakeLock.release(PowerManager.java:300)
06-06 00:44:25.230: E/AndroidRuntime(3855): at com.something.something.SavedGame$5.onClick(SavedGame.java:259)
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.view.View.performClick(View.java:3511)
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.view.View$PerformClick.run(View.java:14105)
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.os.Handler.handleCallback(Handler.java:605)
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.os.Looper.loop(Looper.java:137)
06-06 00:44:25.230: E/AndroidRuntime(3855): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-06 00:44:25.230: E/AndroidRuntime(3855): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 00:44:25.230: E/AndroidRuntime(3855): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 00:44:25.230: E/AndroidRuntime(3855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-06 00:44:25.230: E/AndroidRuntime(3855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-06 00:44:25.230: E/AndroidRuntime(3855): at dalvik.system.NativeStart.main(Native Method)

最佳答案

当您释放锁定时,您的按钮没有任何引用,因为您再次重新初始化按钮并且没有设置 clickListener

更改代码

Button btn=(Button) findViewById(R.id.sleepLock);
PowerManager.WakeLock wl; //This is before my oncreate
int stayAwake = 0; //This is before my oncreate

btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
PowerManager pm = (PowerManager) getSystemService (Context.POWER_SERVICE);
wl = pm.newWakeLock (PowerManager.FULL_WAKE_LOCK, "My Tag");
if (stayAwake == 0) {
btn.setBackgroundResource(R.drawable.awake);
stayAwake = 1;
wl.acquire();
} else {
btn.setBackgroundResource(R.drawable.sleep);
stayAwake = 0;
wl.release();
}
}
});

现在你的代码可以正常工作了

关于java - 尝试释放 WakeLock 时强行关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907916/

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