简体   繁体   English

进度对话框未关闭

[英]Progress dialog not getting dismissed

I want a progress dialog saying "Please Wait" , while my code downloads and parses a xml file but the Progress Dialog is not going, it somehow getting stuck. 我想要一个进度对话框,说“请稍候”,虽然我的代码下载并解析了一个xml文件,但是“进度对话框”没有进行,但是还是卡住了。 I am using AsyncTask, downloading of xml file and parsing are done in the background thread, the code is working if i remove the progress dialog, but in that case you have to wait for about 4 sec to click my button that dynamically creates UI. 我正在使用AsyncTask,在后台线程中完成了xml文件的下载和解析,如果我删除了进度对话框,则代码可以正常工作,但是在那种情况下,您必须等待大约4秒钟才能单击我的动态创建UI的按钮。

private class ParseXML extends AsyncTask<Integer, Integer, Document>{

private Context context;
    private Activity activity;
    private ProgressDialog pd;
    public ParseXML(Activity activity) {
        this.activity = activity;
        context = activity;
        pd = new ProgressDialog(context);
    }




    public void onPreExecute(){
        super.onPreExecute();
    /*  ProgressDialog pd=ProgressDialog.show(XML_PARSER.this,"","Please Wait");*/

    }


    @Override
protected Document doInBackground(Integer... params) {

     DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
    Document dom1=null;
    try {

        //InputStream is=getResources().openRawResource(R.raw.options);
        URL requestURL = new URL("my url");
        URLConnection connection = requestURL.openConnection();
        is = connection.getInputStream();


        DocumentBuilder db = dbf.newDocumentBuilder();
        dom1=db.parse(is);
        Log.i(TAG,"parsing done");

    }

    catch(ParserConfigurationException pce){
        pce.printStackTrace();
    }
    catch(SAXException se){
        se.printStackTrace();
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }


    ParseDocument(dom1); 



    return null;
    }           

    @Override
    public void onPostExecute(Document d){
        if(pd!=null && pd.isShowing()){
               pd.dismiss();
               }
        super.onPostExecute(d);

    }
}

1. Its always advisable to keep the UI work on UI thread, and Non-UI work on Non-UI thread, but from Android version HoneyComb it became a rule. 1.始终建议使UI在UI线程上工作,而非UI在非UI线程上工作,但是从Android版本HoneyComb开始,这已成为一条规则。

2. So as you are using AsyncTask , let you ProgressDialog start showing in the Dedicated UI thread , from the onCreate() method. 2.因此,当您使用AsyncTask ,让您从onCreate()方法开始在专用UI线程中显示 ProgressDialog

3. Then dismiss the ProgressDialog from onPostExecute() Method, which is again on the Dedicated UI thread. 3.然后从onPostExecute()方法关闭 ProgressDialog ,该方法同样位于专用UI线程上。

You are making mistake here 你在这里犯错

 public void onPreExecute(){
  super.onPreExecute();
 /*  ProgressDialog pd=ProgressDialog.show(XML_PARSER.this,"","Please Wait");*/
  }

you are declaring ProgressDialog pd a local variable 您正在声明ProgressDialog pd为局部变量

and when control is coming to 当控制权到来时

@Override
    public void onPostExecute(Document d){
        if(pd!=null && pd.isShowing()){
               pd.dismiss();
               }
        super.onPostExecute(d);

    }

pd is null here as you have not initialize the global pd variable. pd在此处为null,因为您尚未初始化全局pd变量。 that is why alert dialog is not being dismissed. 这就是为什么不关闭警报对话框的原因。

to make this work just make some changes in onPreExecute 使这项工作只需在onPreExecute进行一些更改

public void onPreExecute(){
      super.onPreExecute();
       pd=ProgressDialog.show(XML_PARSER.this,"","Please Wait");
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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