【问题标题】:Why is this ProgressDailog is not displaying?为什么不显示此进度对话框?
【发布时间】:2012-03-18 06:25:41
【问题描述】:

这是我的代码和问题。

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

为什么没有显示进度对话框? 正确的展示位置在哪里?

【问题讨论】:

    标签: android multithreading handler 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);
    
                }
            });       
    

    dismiss() 方法也一样

    【讨论】:

      【解决方案2】:

      您应该使用 AsyncTask 而不是使用线程。 UI 只能从 UI 线程处理。您无法处理来自其他线程的 UI 线程。

      有关这方面的更多信息,请阅读以下链接中的我的博客

      http://pavandroid.blogspot.in/2010/09/how-to-create-calendar-in-android.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多