Android进度对话框未关闭

时间:2013-11-18 09:03:59

标签: java android progressdialog

我想在页面布局中显示进度对话框。我在以下代码中实现。进度对话框未关闭并且一直运行。当我单击上一页中的图像时,它将导航到下一个布局,我希望此布局在从服务器下载所有数据并在列表中显示之前显示进度对话框目前的布局。将显示“进度”对话框,并在后台显示列表,但进度对话框将继续运行且不会关闭。我不知道我哪里错了。请帮助。

ProgressDialog pg;
String[] ar;
public void onCreate(Bundle savedInstanceState) 
{

         super.onCreate(savedInstanceState);
         setContentView(R.layout.filename);
         pg=ProgressDialog.show(this, "ABC", "Downloading .....",true);  

         Thread dt= new Thread(new Runnable() 
        {
            public void run() 
            {
                try
                {
                     String addr=Util.url;
                     URL url = new URL(urlname);
                     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                     urlConnection.setConnectTimeout(5000);             
                     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                     BufferedReader r = new BufferedReader(new InputStreamReader(in));
                     String x = "";
                     String total = "";
                     int i=0;
                     ArrayList<String> content = new ArrayList();
                     while((x = r.readLine()) != null)
                    {
                        content.add(x);
                    }
                    in.close();
                    r.close();
                    ar= content.toArray(new String[content.size()]);

                }
                catch(Exception e1){
                     handler.sendEmptyMessage(0);
                }

            }

        });

        dt.start();
        try{
            dt.join();

        }catch(Exception e){
            handler.sendEmptyMessage(0);
        }

    try{   
        if(ar[0].toString().trim()!="")
        {
             android.view.Display display1 = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
             TableLayout tbl1 = (TableLayout)findViewById(R.id.tableLayout2);

             TableRow newRow1 = (TableRow) new TableRow(this);
             TextView txt=(TextView) new TextView(this);
             txt.setText("No");
             txt.setGravity(Gravity.LEFT);
             txt.setTextColor(Color.RED);
             txt.setTextSize(18);

             TextView txt1=(TextView) new TextView(this);
             txt1.setText("NAME");
             txt1.setTextColor(Color.RED);
             txt1.setTextSize(18);
             txt1.setGravity(3);

             TextView txt2=(TextView) new TextView(this);
             txt2.setText("DATE");

             txt2.setTextColor(Color.RED);
             txt2.setTextSize(18);
             txt.setGravity(3);

             TextView txt3=(TextView) new TextView(this);
             txt3.setText("VALUE");
             txt3.setTextColor(Color.RED);
             txt3.setTextSize(18);
             txt3.setGravity(Gravity.RIGHT);


             txt.setWidth((int)(display1.getWidth()/4));
             txt1.setWidth((int)(display1.getWidth()/4));
             txt3.setWidth((int)(display1.getWidth()/4)); 
             txt2.setWidth((int)(display1.getWidth()/4));


             newRow1.addView(txt2);
             newRow1.addView(txt);
             newRow1.addView(txt1);
             newRow1.addView(txt3);
             tbl1.addView(newRow1);

            for(int t=0;t<(ar.length);t++)
            {
            android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            TableLayout tbl = (TableLayout)findViewById(R.id.tableLayout1);
            TableRow newRow = (TableRow) new TableRow(this);
            newRow.setClickable(true);
            TextView tx=(TextView) new TextView(this);
            String temp=ar[t].toString();
            tx.setText(temp);
            tx.setTextColor(Color.WHITE);
            tx.setGravity(Gravity.LEFT);
            tx.setTextSize(15);

            t=t+1;

            TextView tx1=new TextView(this);
            tx1.setText(ar[t].toString());
            tx1.setGravity(Gravity.LEFT);
            tx1.setTextColor(Color.WHITE);
            tx1.setTextSize(15);


            t=t+1;

            TextView tx2=new TextView(this);
            tx2.setText(ar[t].toString());
            tx2.setGravity(Gravity.LEFT);
            tx2.setTextColor(Color.WHITE);
            tx2.setTextSize(15);

            t=t+1;

            TextView tx3=new TextView(this);
            tx3.setText(ar[t].toString());
            tx3.setGravity(Gravity.RIGHT);
            tx3.setTextColor(Color.WHITE);
            tx3.setTextSize(15);

            tx3.setWidth((int)(display.getWidth()/4));
            tx.setWidth((int)(display.getWidth()/4));
            tx1.setWidth((int)(display.getWidth()/4));
            tx2.setWidth((int)(display.getWidth()/4));

            newRow.addView(tx);
            newRow.addView(tx2);
            newRow.addView(tx1);
            newRow.addView(tx3);

             newRow.setId(t);

            tbl.addView(newRow);
            }

        }
    }
    catch(Exception e){
        pg.dismiss();
        handler.sendEmptyMessage(0); 
    }

}
private Handler handler = new Handler() {
    @Override 
          public void handleMessage(Message msg) {

        Toast.makeText(this,"Network not available!.... ", Toast.LENGTH_LONG).show();
}
};

7 个答案:

答案 0 :(得分:1)

将此行添加到您要关闭对话框的位置。

if(pg.isShowing())pg.dismiss();

答案 1 :(得分:1)

您正在关闭尝试异常的ProgressDialog

catch(Exception e){
        pg.dismiss();
        handler.sendEmptyMessage(0); 
    }

这就是为什么没有Exception就不会关闭

答案 2 :(得分:1)

我已经完成了您的代码,当且仅当您的程序抛出异常时,您的进度对话框才会退出。将它放在catch块的一侧(在catch块之后) 以下代码

catch(Exception e){
    pg.dismiss();
    handler.sendEmptyMessage(0); 
}

应改为

catch(Exception e){
    handler.sendEmptyMessage(0); 
}finally{
    pg.dismiss();
}

如果上面没有工作,请尝试将finally块移动到线程的run方法内,如下所示..

Thread dt= new Thread(new Runnable() 
    {
        public void run() 
        {
            try
            {
                 String addr=Util.url;
                 URL url = new URL(urlname);
                 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                 urlConnection.setConnectTimeout(5000);             
                 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                 BufferedReader r = new BufferedReader(new InputStreamReader(in));
                 String x = "";
                 String total = "";
                 int i=0;
                 ArrayList<String> content = new ArrayList();
                 while((x = r.readLine()) != null)
                {
                    content.add(x);
                }
                in.close();
                r.close();
                ar= content.toArray(new String[content.size()]);

            }
            catch(Exception e1){
                 handler.sendEmptyMessage(0);
            }finally{
                pg.dismiss();
            }

        }

    });

答案 3 :(得分:1)

替换这个:

catch(Exception e){
    pg.dismiss();
    handler.sendEmptyMessage(0); 
}

到此:

catch(Exception e){
    handler.sendEmptyMessage(0); 
}
pg.dismiss();

答案 4 :(得分:1)

尝试使用AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

示例代码:

AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

    @Override
    protected void onPreExecute() {
         //Show UI
         //Start your progress bar

         showProgress();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
         // do your bg process 
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
                      //Show UI
         //dismiss your progress bar
        hideProgress();
    }

};
task.execute((Void[])null);
  

显示和隐藏进度对话框代码

public void showProgress() {
        progressDialog = ProgressDialog.show(this, "",
                "Loading. Please wait...");
        progressDialog.setCancelable(false);
    }

public void hideProgress() {
    if (progressDialog != null && progressDialog.isShowing()) {
        progressDialog.dismiss();
    }

答案 5 :(得分:1)

使用AsyncTask并移动doInBackground()中与网络相关的所有代码。在ProgressDialog的{​​{1}}中显示onPreExecute(),并将其隐藏在AsyncTask中。

onPostExecute()

希望它有所帮助。

答案 6 :(得分:0)

我终于找到了解决方案。我更喜欢使用线程,因为从服务器获取的数据很大,需要动态分配给字段。

Thread thread = new Thread() {

            public void run () {
                 try
                { 
                     pg.show();
                     //long running task
                 }
                catch(){
                }

                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        //code for Update UI  after the long running  task


                        // dismiss the progress dialog on UI thread
                        pg.dismiss();

                    }
                });

            }
        };

        thread.start();