2012-06-06 86 views
1

我有一個按鈕,當你點擊它時,它打開一個WakeLock,並且工作正常!但是當你再次點擊它時,它假設關閉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) 
+1

'只是崩潰'不是一個有意義的錯誤報告。連接ADB,運行logcat併發布詳細的崩潰消息。 –

+0

帖子崩潰報告 – Sameer

+0

@Sameer是的,收購工作正常! –

回答

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

現在您的代碼將正常工作

+0

感謝它幫助我 –