簡體   English   中英

不會在 android 中顯示 progressDialog

[英]won't show progressDialog in android

在按下按鈕后,我必須在 android 項目中的第二個活動中加載進度對話框,但 pogressdialog 沒有加載它。 請問你能幫幫我嗎? 謝謝和對不起我的英語!

代碼是...

ent enviar.setOnClickListener(new OnClickListener()
{
    public void onClick(View v){
        calcularFecha(horaIn,horaFi);
        Runtime runtime = Runtime.getRuntime();
        Log.d("PRUEBA", "COMENZAMOS LA PARTE DE LA CONEXION");
        //getApplicationContext()
        progressDialog = ProgressDialog.show(programacion.this, "", "Loading...");

        new Thread() {
            public void run() {
                try{
                    sleep(10000);
                } catch (Exception e) {
                    Log.d("PRUEBA", e.getMessage());
                }
                // dismiss the progress dialog
                progressDialog.dismiss();
            }
        }.start();

您應該從 UI 線程而不是自定義線程中管理(顯示/刪除)進度對話框。

如果我在當前活動中定義了progressDialog成員,則此解決方案適用於我:

enviar.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        Log.d("PRUEBA", "COMENZAMOS LA PARTE DE LA CONEXION");
        progressDialog = ProgressDialog.show(programacion.this, "", "Loading...");
        calcularFecha(horaIn, horaFi);
        new Thread()
        {
            public void run()
            {
                try
                {
                    sleep(10000);
                }
                catch (Exception e)
                {
                    Log.d("PRUEBA", e.getMessage());
                }
                progressDialog.dismiss();
            }
        }.start();
    }
});

進度對話框顯示 10 秒,然后消失。

您必須確保的是:

  1. 您在programacion活動中(在您的ProgressDialog.show方法中指定)。
  2. calcularFecha(horaIn, horaFi); 不會拋出任何異常。

我認為您的ProgressDialog附加到錯誤的Activity (例如Context )。

您沒有發布完整的 class 源代碼,但問題可能是programacion.this

ProgressDialog.show(programacion.this, "", "Loading...");
ProgressDialog progressDialog= new ProgressDialog(activity.this);
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.setMessage("Please Wait");
progressDialog.show();

if(progressDialog!=null)
{
    progressDialog.dismiss();
    System.out.println("dialog dismissed");
}

嘗試這個

試試這個,如果它適合你..

ProgressDialog update = new ProgressDialog(activity.this);

update.setTitle(getResources().getString(R.string.app_name)); update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); update.setCancelable(true); update.setMax(100); 更新.show();

                    Thread background = new Thread (new Runnable() {
                           public void run() {
                               try {
                                   // enter the code to be run while displaying the progressbar.
                                   //
                                   // This example is just going to increment the progress bar:
                                   // So keep running until the progress value reaches maximum value
                                   while (update.getProgress()<= update.getMax()) {
                                       // wait 500ms between each update
                                       Thread.sleep(500);

                                       // active the update handler
                                       progressHandler.sendMessage(progressHandler.obtainMessage());
                                   }

                               } catch (java.lang.InterruptedException e) {
                                   // if something fails do something smart
                               }
                           }
                        });

                        // start the background thread
                        background.start();
                        if(update.getProgress()== 100)
                           {
                               update.dismiss();
                           }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM