Java Android异步任务运行时异常错误。
我在安卓平台上做解密任务。首先,我创建了一个名为RunDecrypt的方法。当我按下UI中的按钮时,它工作正常。方法如下所示:Java Android异步任务运行时异常错误。,java,android,android-intent,android-listview,android-asynctask,Java,Android,Android Intent,Android Listview,Android Asynctask,我在安卓平台上做解密任务。首先,我创建了一个名为RunDecrypt的方法。当我按下UI中的按钮时,它工作正常。方法如下所示: public void runDecrypt() throws IOException{ EditText editText = (EditText) findViewById(R.id.fileName); EditText editText2 = (EditText) findViewById(R.id.keyName); String f
public void runDecrypt() throws IOException{
EditText editText = (EditText) findViewById(R.id.fileName);
EditText editText2 = (EditText) findViewById(R.id.keyName);
String fileName = editText.getText().toString();
String keyName = editText2.getText().toString();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard , fileName);
File key = new File(sdcard, keyName);
BufferedReader brFile;
String Cipher = null;
try{
//Read file line by line and concat each line of string in file with space character.
FileInputStream fstream = new FileInputStream(file);
brFile = new BufferedReader(new InputStreamReader(fstream));
String tempString;
while((tempString = brFile.readLine()) != null){
if(Cipher == null){
Cipher = tempString;
}else{
Cipher = Cipher.concat(" ");
Cipher = Cipher.concat(tempString);
}
}
}catch(Exception e){
//messageBox("Decrypt", e.getMessage());
}
BufferedReader brKey;
String DecKey = null;
try{
FileInputStream fstream = new FileInputStream(key);
brKey = new BufferedReader(new InputStreamReader(fstream));
DecKey = brKey.readLine();
}catch(Exception e){
//messageBox("Decrypt", e.getMessage());
}
try{
byte[] cipherByte = DES.parseBytes(Cipher);
String decKey = DES.convertStringToHex(DecKey);
byte[] keyByte = DES.parseBytes(decKey);
String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));
String temp = decryptResult.replace(" ", "");
String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
String finResult = DES.convertHexToString(finalDecrypt);
TextView FinalResult = (TextView)findViewById(R.id.decryptText);
FinalResult.setText(finResult);
}catch(Exception e){
messageBox("Decrypt", "Please Upload File Properly");
}
}
private class runDecrypt extends AsyncTask <URL , Integer, Long> {
private final ProgressDialog dialog = new ProgressDialog(Homepage.this);
AlertDialog.Builder builder = new AlertDialog.Builder(Homepage.this);
@SuppressWarnings("resource")
@Override
protected Long doInBackground(URL... params) {
EditText editText = (EditText) findViewById(R.id.fileName);
EditText editText2 = (EditText) findViewById(R.id.keyName);
String fileName = editText.getText().toString();
String keyName = editText2.getText().toString();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard , fileName);
File key = new File(sdcard, keyName);
BufferedReader brFile;
String Cipher = null;
try{
//Read file line by line and concat each line of string in file with space character.
FileInputStream fstream = new FileInputStream(file);
brFile = new BufferedReader(new InputStreamReader(fstream));
String tempString;
try {
while((tempString = brFile.readLine()) != null){
if(Cipher == null){
Cipher = tempString;
}else{
Cipher = Cipher.concat(" ");
Cipher = Cipher.concat(tempString);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(java.io.FileNotFoundException e){
System.out.println("File could not be found.");
}
BufferedReader brKey;
String DecKey = null;
try{
FileInputStream fstream = new FileInputStream(key);
brKey = new BufferedReader(new InputStreamReader(fstream));
try {
DecKey = brKey.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(java.io.FileNotFoundException e){
System.out.println("Key file could not be found.");
}
String decKey = DES.convertStringToHex(DecKey);
byte[] cipherByte = DES.parseBytes(Cipher);
byte[] keyByte = DES.parseBytes(decKey);
String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));
String temp = decryptResult.replace(" ", "");
String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
String finResult = DES.convertHexToString(finalDecrypt);
TextView FinalResult = (TextView)findViewById(R.id.decryptText);
FinalResult.setText(finResult);
return null;
}
protected void onPreExecute() {
this.dialog.setMessage("Decrypting...");
this.dialog.show();
}
protected void onPostExecute(Long result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
builder.setMessage("Decryption Completed");
builder.show();
}
protected void onProgressUpdate(int progress) {
setProgress(progress * 100);
}
}
由于工作正常,我尝试使用异步任务来实现此方法,以便在后台运行我的工作。实现的类如下所示:
public void runDecrypt() throws IOException{
EditText editText = (EditText) findViewById(R.id.fileName);
EditText editText2 = (EditText) findViewById(R.id.keyName);
String fileName = editText.getText().toString();
String keyName = editText2.getText().toString();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard , fileName);
File key = new File(sdcard, keyName);
BufferedReader brFile;
String Cipher = null;
try{
//Read file line by line and concat each line of string in file with space character.
FileInputStream fstream = new FileInputStream(file);
brFile = new BufferedReader(new InputStreamReader(fstream));
String tempString;
while((tempString = brFile.readLine()) != null){
if(Cipher == null){
Cipher = tempString;
}else{
Cipher = Cipher.concat(" ");
Cipher = Cipher.concat(tempString);
}
}
}catch(Exception e){
//messageBox("Decrypt", e.getMessage());
}
BufferedReader brKey;
String DecKey = null;
try{
FileInputStream fstream = new FileInputStream(key);
brKey = new BufferedReader(new InputStreamReader(fstream));
DecKey = brKey.readLine();
}catch(Exception e){
//messageBox("Decrypt", e.getMessage());
}
try{
byte[] cipherByte = DES.parseBytes(Cipher);
String decKey = DES.convertStringToHex(DecKey);
byte[] keyByte = DES.parseBytes(decKey);
String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));
String temp = decryptResult.replace(" ", "");
String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
String finResult = DES.convertHexToString(finalDecrypt);
TextView FinalResult = (TextView)findViewById(R.id.decryptText);
FinalResult.setText(finResult);
}catch(Exception e){
messageBox("Decrypt", "Please Upload File Properly");
}
}
private class runDecrypt extends AsyncTask <URL , Integer, Long> {
private final ProgressDialog dialog = new ProgressDialog(Homepage.this);
AlertDialog.Builder builder = new AlertDialog.Builder(Homepage.this);
@SuppressWarnings("resource")
@Override
protected Long doInBackground(URL... params) {
EditText editText = (EditText) findViewById(R.id.fileName);
EditText editText2 = (EditText) findViewById(R.id.keyName);
String fileName = editText.getText().toString();
String keyName = editText2.getText().toString();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard , fileName);
File key = new File(sdcard, keyName);
BufferedReader brFile;
String Cipher = null;
try{
//Read file line by line and concat each line of string in file with space character.
FileInputStream fstream = new FileInputStream(file);
brFile = new BufferedReader(new InputStreamReader(fstream));
String tempString;
try {
while((tempString = brFile.readLine()) != null){
if(Cipher == null){
Cipher = tempString;
}else{
Cipher = Cipher.concat(" ");
Cipher = Cipher.concat(tempString);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(java.io.FileNotFoundException e){
System.out.println("File could not be found.");
}
BufferedReader brKey;
String DecKey = null;
try{
FileInputStream fstream = new FileInputStream(key);
brKey = new BufferedReader(new InputStreamReader(fstream));
try {
DecKey = brKey.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(java.io.FileNotFoundException e){
System.out.println("Key file could not be found.");
}
String decKey = DES.convertStringToHex(DecKey);
byte[] cipherByte = DES.parseBytes(Cipher);
byte[] keyByte = DES.parseBytes(decKey);
String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));
String temp = decryptResult.replace(" ", "");
String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
String finResult = DES.convertHexToString(finalDecrypt);
TextView FinalResult = (TextView)findViewById(R.id.decryptText);
FinalResult.setText(finResult);
return null;
}
protected void onPreExecute() {
this.dialog.setMessage("Decrypting...");
this.dialog.show();
}
protected void onPostExecute(Long result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
builder.setMessage("Decryption Completed");
builder.show();
}
protected void onProgressUpdate(int progress) {
setProgress(progress * 100);
}
}
我的日志如下所示:
有人能帮忙吗?谢谢,谢谢 您的代码存在各种问题,但特定异常的原因已在Logcat中解释: 只有创建视图层次结构的原始线程才能接触其视图 所讨论的行是
com.example.descracker.Homepage:245
,在这里您可以从异步任务对文本视图调用setText()
。您必须将此逻辑移动到UI线程中,例如通过AsyncTask的工具函数onPreExecute()
,onPostExecute()
或onProgressUpdate()
,或者通过使用post(Runnable)
向视图本身发布延迟操作
另外,请遵循Java编码惯例,并以小写字母开头变量名称。正如Shashank kadhe所提到的,在onPostExecute方法中尝试此方法,并请根据自己的需要进行更改
protected void onPostExecute(String file_url) {
String fileName = editText.getText().toString();
String keyName = editText2.getText().toString();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard , fileName);
File key = new File(sdcard, keyName);
BufferedReader brFile;
String Cipher = null;
try{
//Read file line by line and concat each line of string in file with space character.
FileInputStream fstream = new FileInputStream(file);
brFile = new BufferedReader(new InputStreamReader(fstream));
String tempString;
while((tempString = brFile.readLine()) != null){
if(Cipher == null){
Cipher = tempString;
}else{
Cipher = Cipher.concat(" ");
Cipher = Cipher.concat(tempString);
}
}
}catch(Exception e){
//messageBox("Decrypt", e.getMessage());
}
BufferedReader brKey;
String DecKey = null;
try{
FileInputStream fstream = new FileInputStream(key);
brKey = new BufferedReader(new InputStreamReader(fstream));
DecKey = brKey.readLine();
}catch(Exception e){
//messageBox("Decrypt", e.getMessage());
}
try{
byte[] cipherByte = DES.parseBytes(Cipher);
String decKey = DES.convertStringToHex(DecKey);
byte[] keyByte = DES.parseBytes(decKey);
String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));
String temp = decryptResult.replace(" ", "");
String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
String finResult = DES.convertHexToString(finalDecrypt);
TextView FinalResult = (TextView)findViewById(R.id.decryptText);
FinalResult.setText(finResult);
}catch(Exception e){
messageBox("Decrypt", "Please Upload File Properly");
}
}
Since it's work fine, I try to implement this method with Async Task for running my work in background. The class that implemented shown below:
private class runDecrypt extends AsyncTask <URL , Integer, Long> {
private final ProgressDialog dialog = new ProgressDialog(Homepage.this);
AlertDialog.Builder builder = new AlertDialog.Builder(Homepage.this);
@SuppressWarnings("resource")
@Override
protected Long doInBackground(URL... params) {
EditText editText = (EditText) findViewById(R.id.fileName);
EditText editText2 = (EditText) findViewById(R.id.keyName);
String fileName = editText.getText().toString();
String keyName = editText2.getText().toString();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard , fileName);
File key = new File(sdcard, keyName);
BufferedReader brFile;
String Cipher = null;
try{
//Read file line by line and concat each line of string in file with space character.
FileInputStream fstream = new FileInputStream(file);
brFile = new BufferedReader(new InputStreamReader(fstream));
String tempString;
try {
while((tempString = brFile.readLine()) != null){
if(Cipher == null){
Cipher = tempString;
}else{
Cipher = Cipher.concat(" ");
Cipher = Cipher.concat(tempString);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(java.io.FileNotFoundException e){
System.out.println("File could not be found.");
}
BufferedReader brKey;
String DecKey = null;
try{
FileInputStream fstream = new FileInputStream(key);
brKey = new BufferedReader(new InputStreamReader(fstream));
try {
DecKey = brKey.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(java.io.FileNotFoundException e){
System.out.println("Key file could not be found.");
}
String decKey = DES.convertStringToHex(DecKey);
byte[] cipherByte = DES.parseBytes(Cipher);
byte[] keyByte = DES.parseBytes(decKey);
String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte));
String temp = decryptResult.replace(" ", "");
String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount()));
String finResult = DES.convertHexToString(finalDecrypt);
TextView FinalResult = (TextView)findViewById(R.id.decryptText);
FinalResult.setText(finResult);
}
}
受保护的void onPostExecute(字符串文件\u url){
字符串文件名=editText.getText().toString();
String keyName=editText2.getText().toString();
文件sdcard=Environment.getExternalStorageDirectory();
文件文件=新文件(SD卡,文件名);
文件密钥=新文件(SD卡,密钥名);
缓冲读取文件;
字符串密码=null;
试一试{
//逐行读取文件,并用空格字符表示文件中字符串的每一行。
FileInputStream fstream=新的FileInputStream(文件);
brFile=新的BufferedReader(新的InputStreamReader(fstream));
字符串tempString;
而((tempString=brFile.readLine())!=null){
如果(密码==null){
密码=临时字符串;
}否则{
密码=Cipher.concat(“”);
Cipher=Cipher.concat(tempString);
}
}
}捕获(例外e){
//messageBox(“Decrypt”,例如getMessage());
}
缓冲读取键;
String DecKey=空;
试一试{
FileInputStream fstream=新的FileInputStream(键);
brKey=新的BufferedReader(新的InputStreamReader(fstream));
DecKey=brKey.readLine();
}捕获(例外e){
//messageBox(“Decrypt”,例如getMessage());
}
试一试{
字节[]cipherByte=DES.parseBytes(密码);
字符串decKey=DES.convertStringToHex(decKey);
byte[]keyByte=DES.parseBytes(decKey);
字符串decryptResult=DES.hex(DES.decryptCBC(cipherByte,keyByte));
字符串temp=decryptResult.replace(“,”);
字符串finalDecrypt=temp.substring(0,(temp.length()-DES.getConcatCount());
字符串finResult=DES.convertexhtostring(finalDecrypt);
TextView FinalResult=(TextView)findViewById(R.id.decryptText);
FinalResult.setText(finResult);
}捕获(例外e){
messageBox(“解密”,“请正确上传文件”);
}
}
由于工作正常,我尝试使用异步任务来实现此方法,以便在后台运行我的工作。实现的类如下所示:
私有类runDecrypt扩展异步任务{
private final ProgressDialog=新建ProgressDialog(主页.this);
AlertDialog.Builder=新建AlertDialog.Builder(Homepage.this);
@抑制警告(“资源”)
@凌驾
受保护的长doInBackground(URL…参数){
EditText EditText=(EditText)findViewById(R.id.fileName);
EditText editText2=(EditText)findViewById(R.id.keyName);
字符串文件名=editText.getText().toString();
String keyName=editText2.getText().toString();
文件sdcard=Environment.getExternalStorageDirectory();
文件文件=新文件(SD卡,文件名);
文件密钥=新文件(SD卡,密钥名);
缓冲读取文件;
字符串密码=null;
试一试{
//逐行读取文件,并用空格字符表示文件中字符串的每一行。
FileInputStream fstream=新的FileInputStream(文件);
brFile=新的BufferedReader(新的InputStreamReader(fstream));
字符串tempString;
试一试{
而((tempString=brFile.readLine())!=null){
如果(密码==null){
密码=临时字符串;
}否则{
密码=Cipher.concat(“”);
Cipher=Cipher.concat(tempString);
}
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}catch(java.io.filenotfound异常){
System.out.println(“找不到文件”);
}
缓冲读取键;
字符串DecKey=null;
试一试{
FileInputStream fstream=新的FileInputStream(键);
brKey=新的BufferedReader(新的InputStreamReader(fstream));
试一试{
DecKey=brKey.readLine();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}catch(java.io.filenotfound异常){
System.out.println(“找不到密钥文件”);
}
字符串decKey=DES.convertStringToHex(decKey);
字节[]cipherByte=DES.parseBytes(密码);
byte[]keyByte=DES.parseBytes(decKey);
字符串decryptResult=DES.hex(DES.decryptCBC(cipherByte,keyByte));
字符串temp=decryptResult.replace(“,”);
字符串finalDecrypt=temp.substring(0,(temp.length()-DES.getConcatCount());