One Glove
One Glove

Reputation: 79

Declaring Handler error

I got a code snippet for the Handler in android Java and it tells me to delete the parentheses around the postDelayed parameters.

Handler handler = new Handler();
handler.postDelayed (runable, minute);
private Runnable runnable = new Runnable()
{
    @Override
    public void run()
    {
        biomass = biomass +1;
    }
};

I want the program to increment biomass by 1 after a minute. minute has been declared above as int minute = 60000;

The code snippet I used is here: www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/

Upvotes: 0

Views: 76

Answers (3)

Stephan Celis
Stephan Celis

Reputation: 2612

You can pause your Thread/Runnable with Thread.sleep(6000) where 6000 is number of milliseconds

private Runnable runnable = new Runnable()
{
    @Override
    public void run()
    {
        Thread.sleep(6000);
        biomass = biomass +1;
    }
};

new Thread(runnable).start();

Upvotes: 0

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

You are accessing runnable before defining it.private cant be define there

    Handler handler = new Handler();
    final Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {

        }
    };
    handler.postDelayed (runnable,c);

Upvotes: 0

Saad Asad
Saad Asad

Reputation: 2528

try this:

final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                          public void run() {
                              biomass = biomass +1;
                          }
                        }, 6000);

Upvotes: 1

Related Questions