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:

As was suggested, I implemented runonuithread but it still crashes:

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

}
有帮助吗?

解决方案

I'm guessing this dies on showchar.setText(letArray[i]);. That method requires a java.lang.CharSequence, a char[] or an int resid and you're passing in a single char.


Edit 1

Try something like this:

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

then call:

showchar.setText(tmpArray);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top