在android中实现while循环。

时间:2023-02-10 14:03:38

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

我无法理解android中while循环的实现。

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

每当我在onCreate() bundle中实现while循环时,(代码如下所示)

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:

没有启动,程序在一段时间后进入一个“悬挂”状态,我不明白为什么。Testbyte如下:

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".

updateAuto()应该每1秒更新一次代码,并显示在textView部分中。如下所示,我一直在updateAuto()中使用setText,一切都运行良好,但是一旦实现了while循环,我看到的就是一个黑屏,然后是一个由于它“没有响应”而在几秒钟后强制关闭的选项。

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?

我是否以错误的方式实现了while循环?

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

我也尝试过在一个独立的函数中调用while循环,但是它仍然给了我一个虚无的黑屏。

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?

我读了一些关于处理器服务的东西……它做什么?处理程序服务能以更安全或更有效的方式更新我的TextView吗?

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

如果有人能指点我该怎么做,非常感谢。

1 个解决方案

#1


29  

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.

While循环实际上应该只在单独的线程中实现。一个单独的线程就像在你的应用程序中运行的第二个进程,它之所以关闭是因为你在UI线程中运行了循环,使得UI不能做任何事情,除了通过那个循环。您必须将该循环放置到第二个线程中,以便UI线程可以*运行。当线程化时,除非在UI线程中,否则不能更新GUI。这是在这种情况下的做法。

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:

首先,创建一个Runnable,它将包含在它的run方法中循环的代码。在这个Runnable中,您将不得不创建第二个Runnable,它将发布到UI线程。例如:

 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.

接下来,使用Runnable创建线程并启动它。

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

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

现在,您应该可以看到应用程序在没有强制关闭的情况下循环。

#1


29  

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.

While循环实际上应该只在单独的线程中实现。一个单独的线程就像在你的应用程序中运行的第二个进程,它之所以关闭是因为你在UI线程中运行了循环,使得UI不能做任何事情,除了通过那个循环。您必须将该循环放置到第二个线程中,以便UI线程可以*运行。当线程化时,除非在UI线程中,否则不能更新GUI。这是在这种情况下的做法。

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:

首先,创建一个Runnable,它将包含在它的run方法中循环的代码。在这个Runnable中,您将不得不创建第二个Runnable,它将发布到UI线程。例如:

 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.

接下来,使用Runnable创建线程并启动它。

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

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

现在,您应该可以看到应用程序在没有强制关闭的情况下循环。