这是我的代码&万阿英,蒋达清。
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? 显示它的正确位置在哪里?
答案 0 :(得分:1)
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);
}
});
与dismiss()方法相同的事情
答案 1 :(得分:0)
除了使用Threads之外,您应该使用AsyncTask。 UI只能从UI线程处理。您无法处理来自其他线程的UI线程。
有关这方面的更多信息,请阅读我的博客链接
http://pavandroid.blogspot.in/2010/09/how-to-create-calendar-in-android.html