处理程序&适用于Android计时器的Runnable

时间:2015-04-28 19:40:01

标签: java android handler runnable android-handler

我正在尝试在String中显示一个TextView数组,每次一个项目,每次以毫秒为单位的间隔结束。我已经推荐了这段代码:

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

然而它不起作用,它会抛出ArrayIndexOutOfBoundsException。 我必须手动重新执行split方法(请参阅拆分器),因为split()因某些原因在Android中工作得非常好。我很确定问题不在我的分配器中,因为如果我评论涉及运行runnable的行,它不会崩溃或抛出任何异常。 这是我的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)

提前致谢。

1 个答案:

答案 0 :(得分:1)

你的阅读功能超出界限。如果它在界限内,你没有检查任何东西,只是递增。

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

但如果我正确理解您的问题,您应该使用TextSwitcher,您可以找到示例here。你也可以添加一些非常酷的动画。