为什么ProgressDailog不显示?
问题描述:
这是我的代码& probelm。为什么ProgressDailog不显示?
static Throwable t= null;
static String responseFromServer = "";
static Activity a ;
static Handler mHandler = new Handler();
public static String sendToServer(final Activity act, final String data)
{
progDailog = ProgressDialog.show(act, "", " Please wait...", true);
progDailog.setCancelable(true); //BUT this not displaying
Thread th = new Thread()
{
public void run(){
try{
// .........code ... SENDING data to server
responseFromServer = httpclient.execute(httppost, new BasicResponseHandler()).trim();
mHandler.post(showResponse);
}
catch (Exception e)
{
t = e;
e.printStackTrace();
progDailog.dismiss();
mHandler.post(exception);
}
}
};
th.start();
th.join();
return responseFromServer;
}
private static Runnable showResponse = new Runnable()
{
public void run(){
Toast.makeText(a, responseFromServer, Toast.LENGTH_SHORT).show();
progDailog.dismiss();
}
};
private static Runnable exception = new Runnable()
{
public void run(){
Toast.makeText(a, t + " ", Toast.LENGTH_SHORT).show();
progDailog.dismiss();
}
};
为什么progressdialog没有显示? 哪里是显示它的正确位置?
答
progressDialog.show()只能从UI线程执行。 只是做到以下几点:代替 :
progDailog = ProgressDialog.show(act, "", " Please wait...", true);
使用此代码:
a.runOnUiThread(new Runnable() {
@Override
public void run() {
progDailog = ProgressDialog.show(act, "", " Please wait...", true);
}
});
同样的事情与解雇()方法
答
你应该使用AsyncTask而不是使用线程。 UI只能从UI线程处理。您无法从其他线程处理UI线程。
有关此请阅读我的博客的详细信息在下面的链接
http://pavandroid.blogspot.in/2010/09/how-to-create-calendar-in-android.html
它的工作原理。非常感谢。 – Santhosh 2012-03-19 06:20:11
欢迎您。总是乐于提供帮助 – 2012-03-19 17:36:58