我有一个按钮,当你点击它时,它会打开一个WakeLock,并且工作正常!但是当你再次点击它时,它会假设关闭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
}
});
logcat的:
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)
答案 0 :(得分:2)
当您释放锁定时,您的按钮没有任何参考,因为您再次重新初始化按钮并且未设置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();
}
}
});
现在您的代码可以正常运行