Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8041855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:21:59+00:00 2026-06-05T04:21:59+00:00

I have a button that when you click it, it turns on a WakeLock,

  • 0

I have a button that when you click it, it turns on a WakeLock, and that works fine! But when you click it again its suppose to turn WakeLock off, but instead it just crashes the app! Any help would be appreciates, and I know its not because I’m changing images, because that part works fine! 🙂 Heres my code:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:22:01+00:00Added an answer on June 5, 2026 at 4:22 am

    While you are releasing lock your button does not have any reference as you are again reinitialize button and does not set clickListener

    Change your code

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

    Now your code will work fine

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button that plays an audio file on its click listener. If
Suppose, I want to have a link or a button that when user click
I have a problem that when I click on the image button instead of
okay? I have a button that is inside an updatepanel, click on this button
I have a button click event that takes information from controls and enters it
I have a submit button when i click on that button i am having
I have an app that when I click a button loads a new TabItem
I have a hidden button on a form that I need to click in
I currently have a simple form that when you click the save button will
I have a div that I want when user click on a button slideUp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.