My goal is when the user tap start button, letters "o" "n" "o" "m" and so forth will appear at the center of the screen. "o" will appear first then after a few seconds will be replaced by "n" then "o" and so forth.

note: for brevity, i just make the guessword = onomatopoeia, first. In reality, guessword will changes every time i tap the start bottom.

this is the code:

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play); 

    addStartListener();

}

public void addStartListener(){
    Button start = (Button) findViewById(R.id.start);
    showchar = (TextView) findViewById (R.id.charView);

    start.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Thread thread = new Thread()
            {
                @Override
                public void run() {
                    try {
                        for(int i = 0 ; i < n ; i++) {
                            sleep(1000);
                            showchar.setText(letArray[i]);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            thread.start();

        }



    });

}

thanks for the help

I decided to implement runonuithread but still it crashes:

this is the updated version:

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();

}   
有帮助吗?

解决方案

use this code for setting the text in your textview

               runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                       showchar.setText(letArray[i]);
                    }
                });

其他提示

You are updating ui from a thread which is not possible.

showchar.setText(letArray[i]);

UI must be updated ui thread.

All you are doing is repeatedly setting value to TextView you can use Handler with a delay for this purpose.

You could use runOnUiThread also but i don't see the need for a thread for what you are doing.

Use a Handler. You can find an example @

Android Thread for a timer

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top