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 6369625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:49:46+00:00 2026-05-25T00:49:46+00:00

This has been driving me insane for days, i am new to android and

  • 0

This has been driving me insane for days, i am new to android and i can’t seem to figure out how to access resources from other threads. I am trying to schedule a task to execute after a while, here is the relevant code:

public class TikTakBoom extends Activity {

private SensorManager mSensorManager;

public ToggleButton startButton;

private Bitmap mBombOn;
private Bitmap mBombOff;

/** Tik tak boom **/
protected MediaPlayer mediaPlayer;      // play tik tak or boom
protected Timer boomTimer;              // timer for explosion

// bomb button backgrounds
protected android.graphics.drawable.BitmapDrawable buttonBombOn;
protected android.graphics.drawable.BitmapDrawable buttonBombOff;

// random boom delay in ms
protected long boomDelay = 1500;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // tell system to use the layout defined in our XML file
    setContentView(R.layout.main);

    Log.w(this.getClass().getName(), "SIS is null");

    // configure toggle button (bomb on/off)
    startButton = (ToggleButton)findViewById(R.id.toggleButton1);
    startButton.setText("");
    startButton.setTextOn("");
    startButton.setTextOff("");

    // scale bomb images to button size
    mBombOff = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb_off);
    mBombOff = Bitmap.createScaledBitmap(mBombOff, 120, 120, true);

    mBombOn = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb_on);
    mBombOn = Bitmap.createScaledBitmap(mBombOn, 120, 120, true);

    buttonBombOn = new android.graphics.drawable.BitmapDrawable(mBombOn);
    buttonBombOff = new android.graphics.drawable.BitmapDrawable(mBombOff);

    startButton.setChecked(false);

    boomTimer = new Timer();

    // onclick listener for toggle bomb on/off
    startButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            if (startButton.isChecked()) {
                mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.tik_tak);

                mediaPlayer.setLooping(true);
                mediaPlayer.start();

                // align boom delay with tik tak duration
                boomDelay -= boomDelay%mediaPlayer.getDuration();

                boomTimer.schedule(new BoomTimeTask(getApplicationContext()), boomDelay);

                startButton.setBackgroundDrawable(buttonBombOn);
            } else {
                if (mediaPlayer != null) {
                    mediaPlayer.release();
                    mediaPlayer = null;
                }

                startButton.setBackgroundDrawable(buttonBombOff);
            }
        }

    });
} 

class BoomTimeTask extends TimerTask {
    Context context;

    public BoomTimeTask(Context context) {
        super();

        this.context = context;
    }

    public void run() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }

        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.sheldon);

        mediaPlayer.setLooping(false);
        mediaPlayer.start();

        startButton = (ToggleButton)((Activity)context).findViewById(R.id.toggleButton1);

        startButton.setChecked(false);
        //startButton.setBackgroundDrawable(buttonBombOff);
    }
}

App crashes on startButton.setChecked(false) in BoomTimeTask.run(), i am guessing NullPointerException.

Pls help, i am gonna go insane, i have tried everything! Nothing works.. Tried it without the context passed as a parameter, since the reference should be visible to BoomTimeTask thread as well, tried a dozen of other things, just can’t get it to work.

What am i missing? Thx in advance, would really appreciate any kind of help. Cheers, Val.

SOLVED: how to change button text dynamically for every 3 sec in android?

  • 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-05-25T00:49:46+00:00Added an answer on May 25, 2026 at 12:49 am

    You should use

    runOnUiThread(new Runnable() { 
        public void run() { 
        // YOUR ACTIONS ON Graphic interface
        } 
    }); 
    

    It’s because in android you can’t modify GUI elements without being in the thread that deal with GUI.

    http://developer.android.com/reference/android/app/Activity.html

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

Sidebar

Related Questions

This has been driving me insane. I can't seem to get the RewriteMap directive
This has been driving me mad for a few days... Create a new Window-based
This has been driving be crazy, but I can't seem to find an answer.
he this has been driving me nutts, and I hope someone can shed some
This has been driving me crazy for 2 days. I have been trying to
This has been driving me mad someone help coz i don't seem to see
This has been driving me insane for about the last hour. I'm trying to
This has been driving me nuts for a few days. I am desperate for
This has been driving me crazy for a few days now. I have an
Any help is appreciated this has been driving me insane import wx class Frame(wx.Frame):

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.