ProgressDialog而负载活动 [英] ProgressDialog while load Activity
本文介绍了ProgressDialog而负载活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现这个code在SO显示 ProgressDialog
而负载活动
:
I found this code in SO to show ProgressDialog
while load Activity
:
progDailog = ProgressDialog.show(MyActivity.this, "Process", "please wait....", true, true);
new Thread(new Runnable() {
public void run() {
// code for load activity
}).start();
Handler progressHandler = new Handler() {
public void handleMessage(Message msg1) {
progDailog.dismiss();
}
};
不过,我总是得到此异常:
But I always get this exception:
了java.lang.RuntimeException:无法内螺纹创建处理程序
没有所谓的活套。prepare()
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
我AP preciate针对此问题的任何帮助,先谢谢了。
I appreciate any help for this issue, thanks in advance.
推荐答案
下面是我会做什么:
的AsyncTask做背景的重活
AsyncTask to do the "heavy work" in background:
public class MyTask extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog progressDialog;
public MyTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
//Do your loading here
return "finish";
}
@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
//Start other Activity or do whatever you want
}
}
启动AsyncTask的:
Start the AsyncTask:
MyTask myTask = new MyTask(this);
myTask.execute("parameter");
当然,你可以改变的一般类型的AsyncTask来匹配你的问题。
Of course you can change the generic types of the AsyncTask to match your problems.
这篇关于ProgressDialog而负载活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文