12

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.

Erfan
  • 163
  • 11
Kyle Yeo
  • 2,270
  • 5
  • 31
  • 53
  • 1) The code is **not** `byte testByte == 0;` or it wouldn't compile 2) You are never updating the value of `testByte`, so why would you expect the loop to ever end? – Kirk Woll Sep 20 '11 at 00:56
  • It should actually be testByte = 0; This says you want to assign the value of 0 to the variable testByte. What you did in your example was test for equality (byte testByte == 0;) and apparently this statement evaluated to true and your while loop will always be true so you will be stuck in an infinite loop. – tdavisjr Sep 20 '11 at 01:08
  • yes i thought by doing that it would work actually. i now know i'll need to create another thread to handle it separately so it doesn't get stuck in an infinite loop. – Kyle Yeo Sep 20 '11 at 01:34

2 Answers2

36

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.

Zaid Daghestani
  • 8,555
  • 3
  • 34
  • 44
  • thanks! i can't imagine how to thank you enough, i thought i'd never get past this bug, but your detailed explanation made it clear enough for a beginner like me. thanks again! :D – Kyle Yeo Sep 20 '11 at 01:31
  • 4
    Glad to be of service. Make sure to share the knowledge when you become a pro. :D – Zaid Daghestani Sep 20 '11 at 01:36
  • that thread.sleep and try catch slow down the process very much if you are working with small miliseconds – smoothumut Jun 12 '20 at 17:18
  • 1
    @smoothumut you should look at thread pools. Threads are expensive to create, while Runnables are pretty cheap. A thread pool lets you create pool of threads and submit as many runnables as you'd like. Should take care of your performance issues. – Zaid Daghestani Jan 17 '21 at 01:56
1

You can create a new Thread for a while loop.

This code will create a new thread to wait for a boolean value to change its state.

private volatile boolean isClickable = false;

new Thread() {
    @Override
    public void run() {
        super.run();
        while (!isClickable) {
            // boolean is still false, thread is still running
        }
        // do your stuff here after the loop is finished
    }
}.start();
marticztn
  • 170
  • 1
  • 9