1

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

    }
});

日志猫:

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)
4

1 回答 1

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();
        }
    }
});

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

于 2012-06-06T04:14:19.400 回答