这是我的问题。我正在编写一个android应用程序,它需要显示它正在运行的活动的时间。为此,我创建了一个新的线程,该线程从活动中调用。但是,当我在模拟器中启动应用程序时,似乎Activity从不加载。也许你们可以在这里帮助我。这里是相关的代码:以线程更新UI活动
public class ActivityStartTracker extends ActivityBasic {
TimerThread timer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
timer = new TimerThread();
}
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(
ServiceLocator.BROADCAST_ACTION));
timer.run();
}
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
if (timer.isRunning()) {
timer.interrupt();
}
}
class TimerThread extends Thread {
boolean isRunning = false;
public TimerThread() {
}
@Override
public void run() {
int seconds = 0;
isRunning = true;
while (isRunning) {
try {
Log.d(TAG, "Timer is running for : " + seconds);
seconds++;
Thread.sleep(1000);
} catch (InterruptedException e) {
isRunning = false;
}
}
}
public boolean isRunning() {
return isRunning;
}
}
}
就像我说的,这些是相关的代码片段。 问题是,如果我运行这个,活动保持黑色 - 我只是得到与应用程序的名称头,这就是所有。通过日志记录,我可以确认线程正在运行,但没有其他显示。
你可以请我帮忙,我对android比较陌生。
非常感谢 - 已经提前。
hy!我没有看到你更改gui的代码?我想要在线程中更新gui,您必须使用'runOnUIThread()' – test123123
那是真的 - 在我尝试添加显示定时器的新功能之前,GUI工作正常 - 因此我想我可以删除所有findView()和setText()方法。我只是想展示问题的核心 - 所以你们不必读取所有不必要的代码行。 – Daniel