简体   繁体   中英

Simple while-loop doesnt work

I got a little problem. I think the solution is very simple but unfortunately I can't find it. I hope someone can help me I got a while-loop who has to count up to ten and write the number into a TextView. It still doesn't work ... Thanks for your help! Here is the code:

package de.androidnewcomer.animation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import static android.R.attr.button;
import static de.androidnewcomer.animation.R.id.textView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView ball=(ImageView)findViewById(R.id.ball);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            count();
            break;
    }
}
private void count() {
    TextView textView=(TextView)findViewById(R.id.textView);
    int i;
    i=1;
    while(i<10) {
        i++;
        textView.setText(i);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}

Using setText() with an integer value is for setting a string resource reference. To set the text itself, you have to provide a string: Use setText("" + i); and it should work.

The textView.setText(..) needs a string object, but you use a int. You have to convert your int into a string with the following possible options:

  1. You can use String.valueOf(i) : textView.setText(String.valueOf(i));

  2. You can use Integer.toString(i) : textView.setText(Integer.toString(i));

  3. You can use the empty string literal : textView.setText("" + i);

I prefer the last option. With your code, it should looks like the following code:

private void count() {
   TextView textView=(TextView)findViewById(R.id.textView);
   int i;
   i=1;
   while(i<10) {
       i++;
       textView.setText("" + i);
       try {
           Thread.sleep(200);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
   }
}

You can use a for loop instead of a while loop, like the following code:

private void count() {
   TextView textView=(TextView)findViewById(R.id.textView);

   for(int i = 1; i < 11; i++){ // From 1 to 10
       textView.setText("" + i);
       Thread.sleep(200); 
   }
}

Use a CountDown because you are blocking the main thread

CountDownTimer countDownTimer = new CountDownTimer(2000 /*amount*/, 200/*step*/) {
        public void onTick(long millisUntilFinished) {
           textView.setText("what ever you want");    
        }

        public void onFinish() {
            textView.setText("Done");
        }
    };
countDownTimer.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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