【发布时间】:2014-12-02 14:37:49
【问题描述】:
我有一个使用 run 函数实现 runnable 的类。
@Override
public void run()
{
while(running)
{
// thread code goes here::
threadCode();////
// thread code ends here
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
现在,如果发生某些事情,我会尝试暂停线程。 我尝试使用 wait() 和 notify() 但它使应用程序崩溃,之后我想到使用“正在运行”布尔值并将其设置为 false 作为暂停,然后返回 true 以恢复。 不幸的是,它仍然使应用程序崩溃。
它给了我在 logcat 上的致命错误。
“无法在未调用 Looper.prepare() 的线程内创建处理程序”
有什么简单的方法可以暂停线程吗? ;/ 谢谢
编辑: 这根本与线程无关。 我发现它崩溃的地方。 它在警报对话框中崩溃 这是它的代码:
AlertDialog.Builder builder1 = new AlertDialog.Builder(c);
builder1.setTitle("Game over!");
builder1.setMessage("You survived "+secs+"."+msecs+ " seconds.");
builder1.setCancelable(false);
builder1.setPositiveButton("Replay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
// intialStuff();
dialog.cancel();
}
});
builder1.setNegativeButton("Rage quit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
我尝试在画布类上运行它 也许它不能在画布类上工作? C 是主要活动的上下文。
【问题讨论】:
-
你的 logcat 告诉你什么?
标签: java android multithreading