I wanted to make a program that would quickly read the text from 0 to a huge number. As I just did not try to do it - it does not work. The first option was without displaying the number on the screen and without Thread.Sleep and it worked, but still everything went smoothly and transparently.

public void button2(View view) throws InterruptedException { TextView textbody1 = (TextView)findViewById(R.id.textView4); //почему-то activity крашится, если прописать эту строку там же, где и переменные do { count ++; //переменные типа int объявлены в шапке класса countlast = count+5; textbody1.setText("Число равно:"+count); Thread.currentThread().sleep(150);} while (count < countlast); 

I tried both with smaller numbers and without an infinite loop - the result is the same: the program hangs on both the emulator and the iron device.

Yes, I understand that I’ve gotten pretty nasty somewhere, but I’m just learning, and unfortunately I can only learn in practice, otherwise I won’t remember anything.

It seems to be hanging down and updating the textview, but it hangs when the button is pressed, and I can’t understand anything about the Android Studio logs, except that it eats too many resources. Surely there is some less perverted way to perform this simple task.

  • Your cycle is spinning in the main thread, respectively, until it ends the UI will not be able to update as it also does in the main thread, and you have occupied it with your own cycle. - xkor

3 answers 3

Your cycle will work forever for the reason that the condition will always be met:

 count < countlast 

count will always be less than countlast by 5 because of this line:

 countlast = count+5; 
  • This is done on purpose. The problem is that the text in the textview does not change during the execution of the loop. - FullyRetarded 6:58 pm
  • @ user3807779, this is because you are performing a loop in the main thread, and since the UI is updated in the main thread, then TextVIew will be updated only after executing the code from the main thread. If you want to be updated, move the cycle to a secondary stream, and only TextView update in the main thread - Vladyslav Matviienko

In order not to block the main stream and thus not to block the application interface, you need something like this:

 public void button2(final View view) throws InterruptedException { final TextView textbody1 = (TextView) findViewById(R.id.textView4); // кладем в очередь исполнения следующий код: view.postDelayed(new Runnable() { public void run() { count++; countlast = count + 5; textbody1.setText("Число равно:" + count); if (count < countlast) { // если условие выполняется опять кладем наш Runnable в очередь view.postDelayed(this, 150); } } }, 150); } 
  • I am a noob, and you give me a code with errors. Wednesday swears on the postDelayed syntax. I have not grown to the level of understanding this code. - FullyRetarded 7:37 pm
  • Oh, for a long time the code without lambda did not write, I relaxed completely with retrorolambda, I began to forget the syntax of anonymous classes. And I don’t have the opportunity to run the androyd studio now, so sorry, I’m writing from memory. Corrected like. - xkor
  • @ AlekseyShimansky so he seems to be basing and trying to learn, only the example is not very good for an application with a visual interface, he chose cycles with calculations, this is Hello World for console applications. - xkor

Your cycle will never end, you know that?

The problem is that the program is eating cpu, milling operations in a cycle, because The value passed to the sleep method is in milliseconds, and 150 is too small. Try at least 1000 - 1 second.