我有一个类实现runnable与运行功能。Java/Android线程暂停错误
@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(),但它崩溃了应用程序,之后我想出了使用“running”布尔值并将其设置为false作为暂停,然后返回true以恢复。 不幸的是,它仍然崩溃的应用程序。
它给我logcat致命的错误。
“无法创建内螺纹处理程序尚未调用Looper.prepare()”
有什么工作简单的方法来暂停一个线程? ;/ Thx
编辑: 它根本不是线程。 我发现它崩溃的地方。 它崩溃的警告对话框 这里是它的代码:
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告诉你吗? – 323go 2014-12-02 14:41:35