在“进度”对话框中显示复制文件的文件名(异步任务)

时间:2014-04-22 19:09:15

标签: android android-asynctask progressdialog

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Moving..");
            progressDialog.setIndeterminate(false);
            progressDialog.setMax(100);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setCancelable(false);
            progressDialog.show();
            return  progressDialog;
        default:
            return null;
        }
    }

// AsyncTask for the Progress Dialog and to do Background Process
    private  class myAsyncTask extends AsyncTask<File, String, String> {         
        File sourceFile;         

        @Override         
        protected void onPreExecute() {             
            super.onPreExecute();
            // Shows Progress Bar Dialog and then call doInBackground method
            showDialog(0);

            }

        @Override
        protected String doInBackground(File... params) {

            sourceFile = params[0];
            // code to copy file
    // setting file name of copying filename                
                    **progressDialog.setMessage(children[i]);**
                    publishProgress("" + (count * 100) / children.length);

        }       
        @Override
        protected void onProgressUpdate(String... progress) {

            progressDialog.setProgress(Integer.parseInt(progress[0]));
        }
        @Override         
        protected void onPostExecute(String result) {             
            progressDialog.dismiss();   
            if(result==null)
            {
                Toast.makeText(getApplicationContext(),"File(s) moved",Toast.LENGTH_LONG).show();
            }

        }      

    }

我需要显示复制文件的文件名。我正在使用异步任务在后台复制文件。我能够获取第一个文件的文件名。但之后应用程序崩溃了。有没有办法在ProgressDialog中为此设置文本?

Logcat:

04-23 00:42:26.474:E / AndroidRuntime(26708):在android.app.AlertDialog.setMessage(AlertDialog.java:185) 04-23 00:42:26.474:E / AndroidRuntime(26708):在android.app.ProgressDialog.setMessage(ProgressDialog.java:314) 04-23 00:42:26.474:E / AndroidRuntime(26708):at com.siju.instaclassify.MainActivity $ myAsyncTask.doInBackground(MainActivity.java:357) 04-23 00:42:26.474:E / AndroidRuntime(26708):at com.siju.instaclassify.MainActivity $ myAsyncTask.doInBackground(MainActivity.java:1) 04-23 00:42:26.474:E / AndroidRuntime(26708):在android.os.AsyncTask $ 2.call(AsyncTask.java:264)

1 个答案:

答案 0 :(得分:0)

添加私有变量String ProgressMessage;到你的asynctask类。

 String ProgressMessage= "";

 protected String doInBackground(File... params) {

        sourceFile = params[0];

        ProgressMessage = sourceFile;

        publishProgress("" + (count * 100) / children.length);
  }      

  protected void onProgressUpdate(String... progress) {
      progressDialog.setMessage ( ProgressMessage);
      progressDialog.setProgress(Integer.parseInt(progress[0]));
    }