【问题标题】:AsyncTask progress barAsyncTask 进度条
【发布时间】:2016-03-05 22:25:36
【问题描述】:

我正在使用 AsyncTask(第一次)在文件被加密时显示进度条。在我尝试添加进度条之前,我的加密工作有效,但现在似乎使应用程序崩溃。我知道 onPreExecute() 在测试中有效,所以我认为问题出在 doInBackground() 上。任何帮助将非常感激。

public class EncryptAsync extends AsyncTask<Void, Void, Void> {
    //ProgressDialog progressDialog;

    //declare other objects as per your need
    @Override
    protected void onPreExecute() {
        //   progressDialog = ProgressDialog.show(EncryptFile.this, "Progress Dialog Title Text", "Process Description Text", true);


        if (password.getText().toString().equals(confirmPassword.getText().toString())) {

            correctPassword = password.getText().toString();
            //Toast.makeText(this,correctPassword,Toast.LENGTH_LONG).show();

            //copies Plain Text to String
            fileEditText.setInputType(InputType.TYPE_CLASS_TEXT);
            returnFile = fileEditText.getText().toString();
            Toast.makeText(EncryptFile.this, returnFile, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(EncryptFile.this, "Passwords do not match", Toast.LENGTH_LONG).show();

        }

    }



    @Override
    protected Void doInBackground(Void... params) {


        if (spinnerValue.equals("AES")) {
            Toast.makeText(EncryptFile.this, returnFile, Toast.LENGTH_LONG).show();

            try {
                // Here you read the cleartext.
                FileInputStream fis = new FileInputStream(returnFile);
                // This stream write the encrypted text. This stream will be wrapped by another stream.
                FileOutputStream fos = new FileOutputStream(returnFile + ".aes");

                // hash password with SHA-256 and crop the output to 128-bit for key
                MessageDigest digest = MessageDigest.getInstance("SHA-256");
                digest.update(correctPassword.getBytes());

                // copys hashed password to key
                System.arraycopy(digest.digest(), 0, key, 0, key.length);


                SecretKeySpec sks = new SecretKeySpec(key, "AES");
                // Create cipher
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(Cipher.ENCRYPT_MODE, sks);
                // Wrap the output stream
                CipherOutputStream cos = new CipherOutputStream(fos, cipher);
                // Write bytes
                int b;
                byte[] d = new byte[8];
                while ((b = fis.read(d)) != -1) {
                    cos.write(d, 0, b);
                }
                // Flush and close streams.
                cos.flush();
                cos.close();
                fis.close();
            } catch (Exception ex) {
                Toast.makeText(EncryptFile.this, "Error with Exception", Toast.LENGTH_LONG).show();
            } catch(Throwable t){
                Toast.makeText(EncryptFile.this, "Error with throwable", Toast.LENGTH_LONG).show();
            }

        } else if (spinnerValue.equals("Blowfish")) {
//code for blowfish
        }



         return null;
    }
@Override
    protected void onPostExecute(Void result) {
        Toast.makeText(EncryptFile.this, "Finished Encryption", Toast.LENGTH_LONG).show();
        // super.onPostExecute(result);
        // progressDialog.dismiss();
    }

点击此按钮调用

public void EncryptButton(View view) {

    EncryptAsync task = new EncryptAsync();
    task.execute();

}

【问题讨论】:

  • 如果您需要崩溃方面的帮助,请始终提供崩溃日志
  • 提供崩溃日志。首先,在验证数据并确保所有字段都有效之前不要启动异步任务。
  • 您通常不应该对后台任务进行 UI 操作。尝试从doInBackground 中删除对Toast 的调用

标签: java android android-asynctask


【解决方案1】:

您遇到的异常很可能是 NetworkOnMainThreadException。

在 AsyncTask 中,不允许对 doInBackground 方法中的视图进行任何修改。这应该全部移至 onPreExecute、onPostExecute 或 onUpdate。

至少这条线违反了:

Toast.makeText(EncryptFile.this, returnFile, Toast.LENGTH_LONG).show();

【讨论】:

  • 移除吐司修复了它。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
相关资源
最近更新 更多