实施机器人while循环 [英] Implementing a while loop in android

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

问题描述

我不明白在安卓while循环的实现。

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

每当我实施的onCreate()包内的while循环,(如下图所示code)

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

没有启动,并且该程序进入挂状态一段时间后,我不明白为什么。 Testbyte如下:

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的部分里面的code。我一直在使用的setText内updateAuto(),如下图所示,一切工作正常,但一旦我实现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?

我也试着调用一个单独的函数,而循环,但它仍然给我虚无的黑屏。

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

我一直在阅读一些有关处理程序服务......它有什么作用?能否处理程序服务更新我的的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.

推荐答案

振作起来。并尝试密切关注,这将是非常宝贵的一个dev的。

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

While循环真的应该只在一个单独的线程来实现。一个单独的线程就像是在你的应用程序运行的第二个过程。为什么它强制关闭的原因是因为你运行在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,其中将包含code的循环在其中的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.

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

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