Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a while loop in android

I can't understand the implementation of a while loop in android.

Whenever I implement a while loop inside the onCreate() bundle, (code shown below)

public void onCreate(Bundle icicle) {       
  super.onCreate(icicle);
  setContentView(R.layout.main);
  TextView=(TextView)findViewById(R.id.TextView);
  while (testByte == 0)
      updateAuto(); 
}

nothing boots up, and the program enters a "hanging" state after a while and I can't understand why. Testbyte is as follows:

byte testByte == 0;

and updateAuto() is supposed to update the code per 1 second and display inside the textView portion. I've been using setText inside updateAuto() as shown below and everything works fine, but once i implement the while loop all i see is a black screen and then an option to force close after a few seconds due to it "not responding".

TextView.setText(updateWords);

I've changed it to a button format (meaning i have to click on the button to update itself for now), but i want it to update itself instead of manually clicking it.

Am i implementing the while loop in a wrong way?

I've also tried calling the while loop in a seperate function but it still gives me the black screen of nothingness.

I've been reading something about a Handler service... what does it do? Can the Handler service update my TextView in a safer or memory efficient way?

Many thanks if anyone would give some pointers on what i should do on this.

like image 902
Kyle Yeo Avatar asked Sep 20 '11 00:09

Kyle Yeo


People also ask

What is while loop example?

A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".

What is for loop in Android Studio?

What Is For Loop: In JAVA For statement is the most commonly used lopping statement which iterate over a range of numbers. It is different from if then else in a way that it forces the program to go back up again repeatedly until termination expression evaluate to false.

How do you write a Do While loop?

Here's the basic syntax for a do while loop: do { // body of the loop } while (condition); Note that the test of the termination condition is made after each execution of the loop. This means that the loop will always be executed at least once, even if the condition is false in the beginning.


1 Answers

Brace yourself. And try to follow closely, this will be invaluable as a dev.

While loops really should only be implemented in a separate Thread. A separate thread is like a second process running in your app. The reason why it force closed is because you ran the loop in the UI thread, making the UI unable to do anything except for going through that loop. You have to place that loop into the second Thread so the UI Thread can be free to run. When threading, you can't update the GUI unless you are in the UI Thread. Here is how it would be done in this case.

First, you create a Runnable, which will contain the code that loops in it's run method. In that Runnable, you will have to make a second Runnable that posts to the UI thread. For example:

 TextView myTextView = (TextView) findViewById(R.id.myTextView); //grab your tv
 Runnable myRunnable = new Runnable() {
      @Override
      public void run() {
           while (testByte == 0) {
                Thread.sleep(1000); // Waits for 1 second (1000 milliseconds)
                String updateWords = updateAuto(); // make updateAuto() return a string
                myTextView.post(new Runnable() { 
                     @Override
                     public void run() {
                          myTextView.setText(updateWords);
                     });
           }
      }
 };

Next just create your thread using the Runnable and start it.

 Thread myThread = new Thread(myRunnable);
 myThread.start();

You should now see your app looping with no force closes.

like image 99
Zaid Daghestani Avatar answered Sep 21 '22 08:09

Zaid Daghestani