Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6600983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:41:41+00:00 2026-05-25T18:41:41+00:00

I can’t understand the implementation of a while loop in android. Whenever I implement

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:41:41+00:00Added an answer on May 25, 2026 at 6:41 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you cast a List<int> to List<string> somehow? I know I could loop through
Can I call a java class method from inside a flash movie?
Can i get the source code for a WAMP stack installer somewhere? Any help
Can't work out a way to make an array of buttons in android. This
Can we close all known/unknown connections to database with the code? I'm using Access
Does anyone know how can I replace this 2 symbol below from the string
Can anyone can confirm the best idea for storing jquery code, initially i was
can any one tell me how can change this java code into objective c.is
Can this line of code be made more compact? db_tables_scanned = {} IO.readlines(resume_log_file).each {
Can somebody point me to a resource that explains how to go about having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.