gpt4 book ai didi

Android TimerTask 崩溃程序致命异常 time-0

转载 作者:行者123 更新时间:2023-11-29 22:14:54 24 4
gpt4 key购买 nike

所以我环顾四周,看到了一些关于类似事情的帖子,但它们都与地理定位有关,而我想做的只是一个简单的计时器。基本上我使用 TimerTask 作为 Activity 类中 30 秒的倒计时。每次调用 TimerTask 时应用程序都会崩溃。出于上下文目的,这是来自使用 startActivityForResult 调用调用它的父级的子 Activity 。

public class NewLevelActivity extends Activity {

int time = 30;
int level = 1;
TextView text;
TimerTask task;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_level);
text = (TextView) findViewById(R.id.newLevelText);
level = WholettheballoutActivity.level;


task = new TimerTask() {
public void run() {
time--;
//text.setText("Level " + Integer.toString(level)+ " will start in " +Integer.toString(time) + " seceonds." );
new Timer().schedule(task,1000);
}
};

new Timer().schedule(task,1000);

}


@Override
protected void onPause() {
super.onPause();

}

@Override
protected void onResume() {
super.onResume();
}




}

logcat 显示了这一点

01-03 01:08:57.630: ERROR/AndroidRuntime(19618): FATAL EXCEPTION: Timer-0
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.view.ViewRoot.checkThread(ViewRoot.java:3441)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.view.ViewRoot.requestLayout(ViewRoot.java:586)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.view.View.requestLayout(View.java:10781)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.widget.TextView.checkForRelayout(TextView.java:6052)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.widget.TextView.setText(TextView.java:2826)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.widget.TextView.setText(TextView.java:2691)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at android.widget.TextView.setText(TextView.java:2666)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at org.kizik.WLTBO.NewLevelActivity$1.run(NewLevelActivity.java:43)
01-03 01:08:57.630: ERROR/AndroidRuntime(19618): at java.util.Timer$TimerImpl.run(Timer.java:284)

我认为这是尝试设置 View 文本的问题,但事实并非如此,因为在注释掉设置文本部分后它仍然是错误的

最佳答案

您正在尝试从非 UI 线程更新您的 UI,因此您需要将用于更新 UI 的内容放入 runOnUiThread()

Activity_name.this.runOnUiThread(new Runnable() {
public void run() {
text.setText("Level " + Integer.toString(level)+ " will start in " +Integer.toString(time) + " seceonds." );
}
});

关于Android TimerTask 崩溃程序致命异常 time-0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8709088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com