在android中实现while循环 [英] Implementing a while loop in android

查看:108
本文介绍了在android中实现while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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;

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

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.

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

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.

我一直在阅读有关 Handler 服务的内容……它有什么作用?Handler 服务能否以更安全或内存高效的方式更新我的 TextView?

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.

推荐答案

振作起来.并尝试密切关注,这对于开发人员来说将是无价的.

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

虽然循环确实应该只在单独的线程中实现.一个单独的线程就像在您的应用程序中运行的第二个进程.它强制关闭的原因是因为您在 UI 线程中运行了循环,使得 UI 除了通过该循环外无法执行任何操作.您必须将该循环放入第二个线程中,以便 UI 线程可以自由运行.线程化时,除非您在 UI 线程中,否则无法更新 GUI.这是在这种情况下将如何完成.

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.

首先,您创建一个 Runnable,它将包含在其 run 方法中循环的代码.在该 Runnable 中,您必须创建第二个 Runnable 来发布到 UI 线程.例如:

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);
                     });
           }
      }
 };

接下来只需使用 Runnable 创建线程并启动它.

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.

这篇关于在android中实现while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆