【发布时间】:2015-03-12 08:22:19
【问题描述】:
我收到了java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(),而且,我知道,这意味着我正在尝试在不是 UI 线程的线程中进行一些 UI 修改。让我担心的是,我在 onPreExecute() 的 AsyncTask 中遇到了异常,这就是 UI 线程:
public class CreateDatabaseTask extends AsyncTask<String, Void, Boolean> {
private static final String TAG = "CreateDatabaseTask";
private SQLiteHelper helper;
private ProgressDialog dialog;
private Context context;
public CreateDatabaseTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
helper = new SQLiteHelper(context);
dialog = new ProgressDialog(context); // <--- Just here!
if (!helper.checkExists()) {
dialog.setMessage("Iniciando por primera vez...");
dialog.show();
}
}
@Override
protected Boolean doInBackground(String... params) throws SQLException {
...
}
}
我正在自定义 SQLiteOpenHelper 类中的 public void onUpgrade(...) 中调用 CreateDatabaseTask 构造函数:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
deleteDatabase();
} catch (IOException e) {
e.printStackTrace();
}
CreateDatabaseTask task = new CreateDatabaseTask(context); //<--- I'm calling the constructor here
task.execute();
}
那么,这个AsyncTask 是在欺骗我还是我误会了什么?
【问题讨论】:
-
你在这个调用构造函数中传递了什么
CreateDatabaseTask?以及从哪里执行 AsyncTask ? -
你能发布你的完整的errace跟踪吗
标签: android multithreading android-asynctask runtimeexception