我有一种情况,用户点击特定按钮,我的应用程序中会显示一个以对话框为主题的活动。由于第二个活动(对话框活动)的内容是使用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);