user1488212
user1488212

Reputation: 27

Android : Introducing delay using handler does not work

I have an array of buttons called ButtonList created as follows.

LinearLayout player=(LinearLayout)findViewById(R.id.playerlayout);
        PlayerFirst=(Button)player.getChildAt(0);
        for(int i=0;i<player.getChildCount();i++)
        {
            ButtonList.add((Button)player.getChildAt(i));
        }

I need to sequentially increment the values of those buttons.(with delays in between each change) I have the following code:

for( k=0;k<ButtonList.size();k++)           
{
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
        public void run() {  
            ButtonList.get(getK()).setText(getK()+1+"");
        }
    }, 1000); 
}

Here getK() is the accessor for k in order to access k inside anonymous function.

public int k;
public int getK()
{
    return k;
}

My application crashes. What am I doing wrong?

LogCat Error:

01-03 05:21:04.396: I/Process(1469): Sending signal. PID: 1469 SIG: 9
01-03 05:21:06.276: E/Trace(1535): error opening trace file: No such file or directory (2)
01-03 05:21:08.826: W/dalvikvm(1535): threadid=1: thread exiting with uncaught exception (group=0x2bd39930)
01-03 05:21:08.846: E/AndroidRuntime(1535): FATAL EXCEPTION: main
01-03 05:21:08.846: E/AndroidRuntime(1535): java.lang.IndexOutOfBoundsException: Invalid index 12, size is 12
01-03 05:21:08.846: E/AndroidRuntime(1535):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at java.util.ArrayList.get(ArrayList.java:304)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at com.example.awari.MainActivity$1.run(MainActivity.java:129)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at android.os.Handler.handleCallback(Handler.java:725)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at android.os.Looper.loop(Looper.java:137)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-03 05:21:08.846: E/AndroidRuntime(1535):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 582

Answers (3)

Ruchit Mittal
Ruchit Mittal

Reputation: 312

Your error is because of 'get(getK())', when you are finding the next accessor, I have no idea what you are doing, just put this condition inside FOR loop.

There is no issue with handler.

*if(getK() <= ButtonList.size()) {
     ButtonList.get(getK()).setText(" "+(getK()+1)); 
}*

Upvotes: 1

Sam
Sam

Reputation: 86948

I have an array of buttons called ButtonList. I need to sequentially increment the values of those buttons.(with delays in between each change)

You only need one Handler and Runnable to do what you want. Let's rearrange your code a little.

First make handler a field variable. Then after you initialize ButtonList call:

handler.postDelayed(new Runnable() { 
    public void run() {  
        for(Button button : ButtonList) {
            int count = Integer.parseInt(button.getText());
            button.setText(String.valueOf(count));
        }
        handler.postDelayed(this, 1000); // Run this again in one second
    }
}, 1000); 

This will increment the value of each Button every second.

Upvotes: 0

Jason Robinson
Jason Robinson

Reputation: 31283

It's not happening 1000 milliseconds apart because you're telling it to run ButtonList.size() times with a 1000 millisecond delay. If you start at 0ms, and assuming 1ms computation time between each loop iteration, your updates will happen at 1000ms, 1001ms, 1002ms, etc. You're better off using a TimerTask with a fixed delay.

Upvotes: 0

Related Questions