Android 为什么不显示此ProgressDailog?
这是我的代码和问题Android 为什么不显示此ProgressDailog?,android,multithreading,handler,progressdialog,Android,Multithreading,Handler,Progressdialog,这是我的代码和问题 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 = ProgressDialo
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?
显示它的正确位置在哪里?您应该使用AsyncTask,而不是使用线程。只能从UI线程处理UI。您无法从其他线程处理UI线程 有关这方面的更多信息,请阅读我的博客,链接如下 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);
}
});
dismise()方法也是如此