Android 计时器的处理程序和可运行对象
Handler & Runnable for Android Timer
我正在尝试在 TextView
中显示一个 String
数组,一次显示一个项目,每次以毫秒为单位的间隔结束。我被推荐了这个代码:
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()
在 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.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)
提前致谢。
您的读取功能越界了。您没有检查任何东西是否在范围内,只是递增。
public void read(){
if(i < words.length){
txt.setText(words[i]);
i++;
handler.postDelayed(rnb, interval);
}
}
但是如果我正确理解你的问题,你应该使用TextSwitcher, you can find a sample here。你也可以给它添加一些很酷的动画。
我正在尝试在 TextView
中显示一个 String
数组,一次显示一个项目,每次以毫秒为单位的间隔结束。我被推荐了这个代码:
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()
在 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.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)
提前致谢。
您的读取功能越界了。您没有检查任何东西是否在范围内,只是递增。
public void read(){
if(i < words.length){
txt.setText(words[i]);
i++;
handler.postDelayed(rnb, interval);
}
}
但是如果我正确理解你的问题,你应该使用TextSwitcher, you can find a sample here。你也可以给它添加一些很酷的动画。