progressDialog.dismiss()之后不会解除progressDialog;

时间:2017-02-22 08:20:57

标签: java android android-studio progressdialog

我有一个方法从文件中获取一个字符串并将其放在名为username的String变量中,因为从文件中读取需要时间,我想要一个progressDialog出现,直到函数完成,preogress对话框确实出现但是当我使用progressDialog.dismiss();

时,我不会被解雇

这是代码:

public String loadUserFromFile(){

    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Please wait...");
    progressDialog.setMessage("Loading User");
    progressDialog.show();

    String username = "default";
    String fileName = "savedUserFile.txt";
    FileInputStream fis;
    try {
        fis = openFileInput(fileName);
        InputStreamReader inputStreamReader = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(inputStreamReader);
        username = br.readLine();
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    username = username.replaceAll("\\s+","");
    progressDialog.dismiss();
    return username;
}

1 个答案:

答案 0 :(得分:0)

确保对话框不为空的第一件事。

 ProgressDialog mProgressDialog;
     mProgressDialog = new ProgressDialog(thisActivity);
            mProgressDialog.setCancelable(false);

 //If you want to show progress dialog
     public void showProgressDialog(String message) {
            if (mProgressDialog != null) {
                mProgressDialog.setMessage(message);
                mProgressDialog.show();
            }
        }

   //To dismiss progress dialog
    public void dismissProgressDialog() {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
        }

用法:

showProgressDialog( “加载”);

dismissProgressDialog();