简体   繁体   中英

Handler & Runnable for Android Timer

I'm trying to display a String array in a TextView , one item at a time, every time an interval in milliseconds ends. I've been recommended this code:

package com.tt.blanker;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import java.util.StringTokenizer;

/**
 * Created by Gonzalo on 08/04/2015.
 */
public class ReadActivity extends Activity {
    int i;
    int interval = 200;
    String[] words;
    TextView txt;
    Handler handler = new Handler();
    Runnable rnb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.read);
        String x = getIntent().getExtras().getBundle("b").getString("text");
        words = splitter(x, " ");
        rnb = new Runnable() {
            @Override
            public void run() {
                read();
            }
        };

        txt = (TextView) findViewById(R.id.word);
        txt.setText(words[0]);
        i=0;
        Button fast = (Button) findViewById(R.id.fast);
        Button slow = (Button) findViewById(R.id.slow);
        Button big = (Button) findViewById(R.id.big);
        Button small = (Button) findViewById(R.id.small);
        Button play = (Button) findViewById(R.id.play);
        rnb.run();
    }

    public void read(){
        txt.setText(words[i]);
        i++;
        handler.postDelayed(rnb, interval);
    }

    public String[] splitter(String str, String regex){
        str=str.trim();
        String[] q;
        int length=0;
        for (int i=0; i<str.length(); i++){
            String rx=str.substring(i,i+1);
            if(rx.equals(regex)){
                length++;
            }
        }
        q = new String[length+1];
        for (int p=0; p<q.length; p++){
            q[p]="";
        }
        int actualp=0;
        for (int i=0; i<str.length(); i++){
            String rx=str.substring(i,i+1);
            if(rx.equals(regex)){
                actualp++;
            }
            else{

                q[actualp]+=rx;
            }
        }
        return q;
    }
}

however it does not work, it throws an ArrayIndexOutOfBoundsException . I had to re-do the split method (see splitter) by hand because split() works awfully in Android for some reason. I'm pretty sure that the problem is not in my splitter because if I comment the lines involving running the runnable, it does not crash or throw any exceptions. Here's my logcat:

04-28 16:05:51.436: E/AndroidRuntime(7789): in writeCrashedAppName, pkgName :com.tt.blanker
04-28 16:05:51.436: D/AndroidRuntime(7789): file written successfully with content: com.tt.blanker StringBuffer : ;com.tt.blanker
04-28 16:05:51.436: E/AndroidRuntime(7789): FATAL EXCEPTION: main
04-28 16:05:51.436: E/AndroidRuntime(7789): Process: com.tt.blanker, PID: 7789
04-28 16:05:51.436: E/AndroidRuntime(7789): java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
04-28 16:05:51.436: E/AndroidRuntime(7789):     at com.tt.blanker.ReadActivity.read(ReadActivity.java:48)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at com.tt.blanker.ReadActivity$1.run(ReadActivity.java:32)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at android.os.Handler.handleCallback(Handler.java:733)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at android.os.Looper.loop(Looper.java:136)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at android.app.ActivityThread.main(ActivityThread.java:5021)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at java.lang.reflect.Method.invoke(Method.java:515)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-28 16:05:51.436: E/AndroidRuntime(7789):     at dalvik.system.NativeStart.main(Native Method)

Thanks in advance.

You went out of bounds in your read function. You were not checking anything if it is within the bounds, just incrementing.

public void read(){
        if(i < words.length){
          txt.setText(words[i]);
          i++;
          handler.postDelayed(rnb, interval);
       }
    }

But if I understand your problem correctly, you should use TextSwitcher , you can find a sample here . You can add some pretty cool animations to it too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM