2012-03-18 200 views
0

這是我的代碼& 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沒有顯示? 哪裏是顯示它的正確位置?

回答

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); 

      } 
     });  

同樣的事情與解僱()方法

+0

它的工作原理。非常感謝。 – Santhosh 2012-03-19 06:20:11

+0

歡迎您。總是樂於提供幫助 – 2012-03-19 17:36:58