Android开发:使用runOnUiThread更新ui textview,崩溃

时间:2014-04-26 01:55:24

标签: android user-interface textview

我的目标是当用户点击开始按钮,字母" o" " N" " O" " M"等等将出现在屏幕的中央。 " O"将首先出现,然后几秒后将被" n"然后" o"等等。

注意:为简洁起见,我首先制作guessword = onomatopoeia。实际上,每次点击开始底部时,guessword都会改变。

这是代码:

正如所建议的,我实现了runonuithread,但它仍然崩溃:

public class PlayActivity extends ActionBarActivity {

private String guessword = "onomatopoeia";
private TextView showchar;
private int n = guessword.length();
private char letArray[]= guessword.toCharArray();
private Handler handler;
private int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play); 
    handler = new Handler();
    showchar = (TextView) findViewById (R.id.charView);
}

public void startGame(View view){
    new Thread() {
        public void run() {
            while(i++ < n) {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                             showchar.setText(letArray[i]);
                        }
                    });
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();

}

1 个答案:

答案 0 :(得分:0)

我猜测这会在showchar.setText(letArray[i]);上消失。该方法需要java.lang.CharSequencechar[]int resid,并且您需要传递一个char


编辑1

尝试这样的事情:

char[] tmpArray = new tmpArray[1];
tmpArray[0] = letArray[i];

然后致电:

showchar.setText(tmpArray);