[英]What am I missing in this simple android code?
I was trying to do something else but kept on removing code thinking that simplifying things will help in identifying my mistake. 我试图做其他事情,但是一直在删除代码,以为简化事情将有助于识别我的错误。 This is what I am left with (after a lot of simplification) and still I am not getting it why it is not doing it is supposed to!
这是我所剩下的(经过大量简化之后),但我仍然不明白为什么它不这样做应该是为什么!
public class MyOwnTimerActivity extends Activity {
int timeLeft = 5;
TextView timerTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button timerButton = (Button) findViewById(R.id.button1);
timerTextView = (TextView) findViewById(R.id.timerText);
Thread timer = new Thread() {
public void run() {
try {
sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally{
timerTextView.setText("something");
}
}
};
timer.start();
}
}
Error: The application crashed after 3 seconds ie after timer goes off. 错误:应用程序在3秒钟后崩溃,即计时器关闭之后。
Ah, yes)) This is because you try to modify UI from non UI-thread 嗯,是))这是因为您尝试从非UI线程修改UI
There are several ways to modify UI from another thread: 有几种方法可以从另一个线程修改UI:
Handler
class with overriding handleMessage()
method Handler
类与重载handleMessage()
方法一起使用 Activity
class with calling runOnUI()
method, passing there Runnable
object in which run()
method you modify UI Activity
类与调用runOnUI()
方法一起使用,向其中传递您在其中修改UI的run()
方法的Runnable
对象 View.post()
or View.postDelayed()
methods View.post()
或View.postDelayed()
方法 The last variant is the most convenient in your case. 最后一种变型是您所用的最方便的。 Modify your code in such manner:
以这种方式修改您的代码:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button timerButton = (Button) findViewById(R.id.button1); timerTextView = (TextView) findViewById(R.id.timerText); timerTextView.postDelayed( new Runnable() { @Override run() { timerTextView.setText("something"); } }, 3000); }
You're not supposed to change UI from worker threads. 您不应该从辅助线程更改UI。 Use a Handler, initialized on the main thread, with an ad-hoc Runnable to execute the setText() on the main thread.
使用在主线程上初始化的Handler和临时Runnable在主线程上执行setText()。
Handler h = new Handler();
Thread timer = new Thread() {
//...
finally
{
h.post(new Runnable(){
public void run()
{
timerTextView.setText("something");
}
});
}
In fact, the whole snippet can be rewritten using Handler.postDelayed(). 实际上,可以使用Handler.postDelayed()重写整个代码段。 But you'll probably want to do more interesting things with a thread eventually.
但是您最终可能会想用线程做更多有趣的事情。
Have you tried using runOnUI to set the Text from the UI thread? 您是否尝试过使用runOnUI从UI线程设置Text? You can do so like this:
您可以这样做:
runOnUiThread(new Runnable() {
public void run()
{
timerTextView.setText("something");
}
});
Keep in mind, your reference to timerTextView and whatever text you want displayed would have to be modified as a final variable in order to reference it from another thread. 请记住,您对timerTextView的引用以及要显示的任何文本都必须修改为最终变量,以便从另一个线程引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.