我有一个按钮,当您单击它时,它将打开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)

最佳答案

释放锁定时,您的按钮没有任何引用,因为您再次重新初始化按钮,并且未设置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();
        }
    }
});


现在您的代码可以正常工作

10-08 03:36