gpt4 book ai didi

java - 延迟一段时间后跳转到方法时出错

转载 作者:行者123 更新时间:2023-11-29 21:34:21 25 4
gpt4 key购买 nike

我正在开发一个基于测验的应用程序,在这个应用程序中,我试图在一些延迟时间后跳转到某种方法,但不幸的是我遇到了一些运行时错误..这是代码..

new Timer().schedule(new TimerTask() {          
@Override
public void run()
{
resetcolor();
nextpage();
rg.clearCheck();
showdata();
}
}, 6000);

这是我的 Log cat Error..

09-12 12:11:06.775: W/dalvikvm(489): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-12 12:11:06.789: E/AndroidRuntime(489): FATAL EXCEPTION: Timer-0
09-12 12:11:06.789: E/AndroidRuntime(489): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-12 12:11:06.789: E/AndroidRuntime(489): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.view.View.invalidate(View.java:5115)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.widget.TextView.invalidateDrawable(TextView.java:3796)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:300)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:227)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:97)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.graphics.drawable.Drawable.setState(Drawable.java:400)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.widget.CompoundButton.drawableStateChanged(CompoundButton.java:271)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.view.View.refreshDrawableState(View.java:7248)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.widget.CompoundButton.setChecked(CompoundButton.java:115)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.widget.RadioGroup.setCheckedStateForView(RadioGroup.java:179)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.widget.RadioGroup.check(RadioGroup.java:159)
09-12 12:11:06.789: E/AndroidRuntime(489): at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
09-12 12:11:06.789: E/AndroidRuntime(489): at com.example.quizapp.Firstques$1$1.run(Firstques.java:234)
09-12 12:11:06.789: E/AndroidRuntime(489): at java.util.Timer$TimerImpl.run(Timer.java:289)

最佳答案

重写你的代码如下:

new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(6000);
runOnUiThread(new Runnable() {
@Override
public void run() {
resetcolor();
nextpage();
rg.clearCheck();
showdata();
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}).start();

您的问题是您无法从后台线程触摸 UI 元素。上面的代码在主线程(也称为“UI 线程”)中执行 UI 更改。

关于java - 延迟一段时间后跳转到方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18757492/

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