我有一个扩展Thread
的课程。该类将读取bluetooth
上的一些字节,一旦完成或读取最后一个字节,它将调用event listener
以更新所有监听类。
在蓝牙课上就像这样:
fileCompleteInitiator.fileCompleted(fileName);
实现的方法创建AsyncTask
以处理此文件:
public class Home extends Activity {
//other methods like onCreate..
@Override
public void fileComplete(String fileName) {
if(fileName.endsWith(".zip")) {
CaseGenerator generator = new CaseGenerator(Home.this, fileName, lastCases, nist);
generator.execute("");
}
}
}
在这个asynctask中,我想展示一个progressdialog
,但我得到了不那么原始的异常:
Cant create handler inside thread that has not called looper.prepare
它的代码是常见的,在progressdialog
中创建onPreExecute()
并在onPostExecute()
中将其关闭。一些代码
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(context);
pd.setTitle("Please Wait..");
pd.setMessage("Generating file");
pd.show();
}
.....................
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
if(pd != null)
pd.dismiss();
}
我错过了什么?
答案 0 :(得分:1)
您需要从主应用程序线程创建并执行AsyncTask
。无论如何,这是AsyncTask
的要求,最后一次检查,它将解决您的onPreExecute()
问题。