【发布时间】:2015-02-27 12:10:51
【问题描述】:
众所周知,android 对话框只能在主 UI 线程下创建和显示。
问题是我有一个进度条,并且在一个线程中完成了很多工作,在进度的某些部分我必须显示一个对话框,并且用户必须选择一个选项才能继续工作。
所以.. 我需要在线程中显示一个对话框(阻塞线程直到用户选择了一个选项)。
我用这段代码试过了
public void showDialog(Activity activity) {
Looper.prepare();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
// set title
alertDialogBuilder.setTitle("");
// set dialog message
alertDialogBuilder
.setMessage(R.string.STREAM_NOT_WIFI)
.setCancelable(false)
.setPositiveButton(R.string.STREAM_NOT_WIFI_YES, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
preload=true;
}
})
.setNegativeButton(R.string.CANCEL, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
preload=false;
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
但我收到了这条消息,但没有显示对话框:
"attempting to initialize hardware acceleration outside of the main thread, aborting"
【问题讨论】:
标签: android multithreading dialog android-dialog hardware-acceleration