简体   繁体   English

Android计时器的处理程序和可运行

[英]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. 我试图在TextView中一次显示一个String数组,每次一次间隔以毫秒为单位时,一次显示一项。 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 . 但是它不起作用,它抛出ArrayIndexOutOfBoundsException I had to re-do the split method (see splitter) by hand because split() works awfully in Android for some reason. 我不得不手动重新执行split方法(请参阅splitter),因为split()由于某种原因在Android中无法正常工作。 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. 我非常确定问题不在我的分离器中,因为如果我注释涉及运行runnable的行,则它不会崩溃或引发任何异常。 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 . 但是,如果我正确理解了您的问题,则应该使用TextSwitcher ,您可以在此处找到一个示例。 You can add some pretty cool animations to it too. 您也可以添加一些很酷的动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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