Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么不显示此ProgressDailog?_Android_Multithreading_Handler_Progressdialog - Fatal编程技术网

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()方法也是如此