我有一种情况,用户点击一个特定的按钮,并在我的应用程序中显示一个对话主题活动。由于使用HttpRequest
获取第二个活动(对话框活动)的内容,我希望用户看到ProgressDialog
,直到加载对话框。
AsyncTask似乎是用于此目的的最佳选择。因此我写了下面的课。
public class DetailsList extends AsyncTask<String, Integer, String> {
public static final String LOG_TAG = "DetailsList";
private Context context;
private ProgressDialog dialog;
private Activity activity;
public DetailsList(Context context) {
this.context = context;
}
public DetailsList(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(this.context);
dialog.setMessage("Loading...");
dialog.setCanceledOnTouchOutside(false);
}
@Override
protected void onPostExecute(String result) {
if(dialog.isShowing()) {
dialog.dismiss();
}
}
@Override
protected String doInBackground(String...params) {
this.publishProgress(100);
Log.d(LOG_TAG, "doInBackground started.");
JSONObject requestContent = new JSONObject();
JSONObject detailViewResponse = new JSONObject();
try {
requestContent.put("serviceId", params[0]);
APIRequest detailViewAPI = new APIRequest(
"http://" + GeneralConstants.SERVER_ADDRESS + "/services/",
requestContent
);
detailViewResponse = detailViewAPI.request();
} catch (Exception e) {
e.printStackTrace();
}
return detailViewResponse.toString();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.show();
}
}
当进行get调用时,我有以下异常堆栈跟踪:
12-29 05:12:29.149: W/System.err(3459): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-29 05:12:29.159: W/System.err(3459): at android.os.Handler.<init>(Handler.java:121)
12-29 05:12:29.159: W/System.err(3459): at android.app.Dialog.<init>(Dialog.java:112)
12-29 05:12:29.159: W/System.err(3459): at android.app.AlertDialog.<init>(AlertDialog.java:114)
12-29 05:12:29.159: W/System.err(3459): at android.app.AlertDialog.<init>(AlertDialog.java:98)
12-29 05:12:29.164: W/System.err(3459): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
12-29 05:12:29.164: W/System.err(3459): at threads.DetailsList.<init>(DetailsList.java:30)
12-29 05:12:29.164: W/System.err(3459): at app.command.DetailViewDisplayCommand.execute(DetailViewDisplayCommand.java:54)
12-29 05:12:29.164: W/System.err(3459): at commands.Command.execute(Command.java:32)
12-29 05:12:29.164: W/System.err(3459): at gl.ObjectPicker.findObjectForValue(ObjectPicker.java:92)
12-29 05:12:29.164: W/System.err(3459): at gl.ObjectPicker.pickObject(ObjectPicker.java:58)
12-29 05:12:29.164: W/System.err(3459): at gl.GL1Renderer.onDrawFrame(GL1Renderer.java:111)
12-29 05:12:29.164: W/System.err(3459): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1520)
12-29 05:12:29.164: W/System.err(3459): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1244)
似乎错误是从我初始化对话框的行触发的。
dialog = new ProgressDialog(this.context);
答案 0 :(得分:3)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1244)
AsyncTask旨在防止主线程因艰苦的操作而变慢。但是你不在主线程上,你在OpenGL线程上。我自己没试过,但是会发生什么:
@Override
protected void onPreExecute() {
Looper.prepare();
Looper.loop();
dialog = new ProgressDialog(this.context);
dialog.setMessage("Loading...");
dialog.setCanceledOnTouchOutside(false);
}
@Override
protected void onPostExecute(String result) {
if(dialog.isShowing()) {
dialog.dismiss();
}
Looper.quit();
}
(这可能有用,或者它可能会尝试从错误的线程异常中访问UI。)