有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ProgressDialog:显示后不解除

    private void copyFile(AppDB apkfile) {
    ProgressDialog pd=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
    pd.show(this,"Coping ...",apkfile.name,true,true);
    File f1 = new File(apkfile.location);
    try {
        String fileName = apkfile.name;
        File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
        f2.mkdirs();
        f2 = new File(f2.getPath() + "/" + fileName + ".apk");
        f2.createNewFile();
        InputStream in = new FileInputStream(f1);
        OutputStream out = new FileOutputStream(f2);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException ex) {
        Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);

    } catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
    }
    if(pd.isShowing()) {
   pd.dismiss();}

}

大家好,当试图关闭进度对话框时,它不会这样做,并研究了许多解决方案,但我不知道它的相同问题。 请帮忙

更新:运行后有进度对话框pd的类

Screenshot 1


共 (3) 个答案

  1. # 1 楼答案

    复制应该在线程或异步任务中完成。在执行AsyncTask之前显示对话框并隐藏在onPOstExecute中

    protected void onPostExecute(Long result) {
           if(mProgressDialog.isShowing()) {
              mProgressDialog.dismiss();}
           }  
    }
    
  2. # 2 楼答案

    尝试使用finally

    private void copyFile(AppDB apkfile) {
        ProgressDialog pd=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER);
        pd.show(this,"Coping ...",apkfile.name,true,true);
        File f1 = new File(apkfile.location);
        try {
            String fileName = apkfile.name;
            File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
            f2.mkdirs();
            f2 = new File(f2.getPath() + "/" + fileName + ".apk");
            f2.createNewFile();
            InputStream in = new FileInputStream(f1);
            OutputStream out = new FileOutputStream(f2);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException ex) {
            Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);
    
        } catch (IOException e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
        } finally {
          if(pd.isShowing()) {
            pd.dismiss();
          }
        }
    }
    
  3. # 3 楼答案

    它运行良好,我将告诉你两种可能性

    1. 最后请在try块中使用它作为

      try {
          String fileName = apkfile.name;
          File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share");
          f2.mkdirs();
          f2 = new File(f2.getPath() + "/" + fileName + ".apk");
          f2.createNewFile();
          InputStream in = new FileInputStream(f1);
          OutputStream out = new FileOutputStream(f2);
          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0) {
              out.write(buf, 0, len);
          }
          in.close();
          out.close();
         if(pd.isShowing()) {
         pd.dismiss();}
          } catch (FileNotFoundException ex) {
              Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT);
         } catch (IOException e) {
          Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
      }
      

      }

    二是 延期使用