简体   繁体   中英

won't show progressDialog in android

I have a to load a progressdialog in a second activity that I have in my android project after push a button but the pogressdialog doesnt load it. Could you help me please? Thanks and sorry for my english!

The code is...

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

You should manage (show / remove) the progress dialog from within the UI thread instead of your custom thread.

This solution works for me, if I have the progressDialog member defined inside my current activity:

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

The progress dialog is shown for 10 seconds, then it gets dismissed.

What you have to make sure, is:

  1. your are inside the programacion activity (that is specified in your ProgressDialog.show method).
  2. The calcularFecha(horaIn, horaFi); doesn't throw any exception.

I think your ProgressDialog is attached to the wrong Activity (eg Context ).

You did not post the full class source code, but the problem could be 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");
}

try this

Try this if it works for you..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM