Java Android异步任务总是崩溃
很抱歉出现这样的问题,但我已经尝试了几个小时来解决它,但它不会起作用。我正在使用Java Android异步任务总是崩溃,java,android,crash,android-asynctask,Java,Android,Crash,Android Asynctask,很抱歉出现这样的问题,但我已经尝试了几个小时来解决它,但它不会起作用。我正在使用AsyncTask下载一个文件并显示进度 我已经在我的应用程序中使用过一次了,而且效果很好,所以我只是将代码复制到另一个类中并进行了一些编辑。但无论我做什么,当我点击按钮下载时,它崩溃了 我确实有互联网权限,也有向外部存储器写入的权限。代码如下: BootAnimation.class: package com.cydeon.plasmamodz; import java.io.BufferedInputStrea
AsyncTask
下载一个文件并显示进度
我已经在我的应用程序中使用过一次了,而且效果很好,所以我只是将代码复制到另一个类中并进行了一些编辑。但无论我做什么,当我点击按钮下载时,它崩溃了
我确实有互联网权限,也有向外部存储器写入的权限。代码如下:
BootAnimation.class
:
package com.cydeon.plasmamodz;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeoutException;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;
import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class BootAnimation extends Activity{
String fileName;
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... sURL) {
try{
URL url = new URL(sURL[0]);
URLConnection connection = url.openConnection();
connection.connect();
//Shows 0-100% progress bar
int fileLength = connection.getContentLength();
//Download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/plasma/boot/bootanimation.zip");
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
//Publish the Progress
publishProgress((int) (total * 100/fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress){
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
mProgressDialog.dismiss();
Context context = getApplicationContext();
CharSequence text = "Installing " + fileName;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
CommandCapture command = new CommandCapture(0, "su", "#!/system/bin/sh", "busybox mount -o remount, rw /system", "cd /sdcard/plasma/boot", "rm /system/media/bootanimation.zip", "cp /sdcard/plasma/boot/bootanimation.zip /system/media", "chmod 644 /system/media/bootanimation.zip", "busybox killall system_server");
try {
RootTools.getShell(true).add(command).waitForFinish();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
}
}
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
ProgressDialog mProgressDialog;
ImageView batteryView = (ImageView) findViewById(R.id.ivBattery);
Bundle battery;
battery = getIntent().getExtras();
final String android = battery.getString("Android");
final String dragon = battery.getString("Dragon");
final String gameboy = battery.getString("GameBoy");
final String gamecube = battery.getString("GameCube");
final String nexus = battery.getString("Nexus");
final String nightmare = battery.getString("Nightmare");
final String xbox = battery.getString("Xbox");
final String xbox1 = battery.getString("Xbox1");
if (android != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "https://dl.dropbox.com/s/kl2bbv7vp2sq9mh/0038.jpg", R.drawable.default_img, 60000);
}else if (dragon != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "http://farm9.staticflickr.com/8158/7454334802_31641ae875_m.jpg", R.drawable.default_img, 60000);
}else if (gameboy != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "http://farm9.staticflickr.com/8153/7457111720_6d74056a02_m.jpg", R.drawable.default_img, 60000);
}else if (gamecube != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "http://farm9.staticflickr.com/8015/7457111876_ca01535a4d_m.jpg", R.drawable.default_img, 60000);
}else if (nexus != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "http://farm9.staticflickr.com/8149/7462059092_86088934b2_m.jpg", R.drawable.default_img, 60000);
}else if (nightmare != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "http://farm9.staticflickr.com/8014/7490899268_6b812e99f4_m.jpg", R.drawable.default_img, 60000);
}else if (xbox != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "http://farm8.staticflickr.com/7254/7457111536_4d1924a1d4_m.jpg", R.drawable.default_img, 60000);
}else if (xbox1 != null){
UrlImageViewHelper.setUrlDrawable(batteryView, "http://farm9.staticflickr.com/8012/7457111956_8ca4ee912f_m.jpg", R.drawable.default_img, 60000);
}
mProgressDialog = new ProgressDialog(BootAnimation.this);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setMessage("Downloading " + fileName);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
Button bInstallB = (Button) findViewById(R.id.bInstallBattery);
Button bReturnB = (Button) findViewById(R.id.bReturnBattery);
bInstallB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (android != null){
fileName = "Android Boot Animation";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("https://dl.dropbox.com/s/mqan6ly3g8t4r5j/bootanimation.zip");
}if (dragon != null){
fileName = "Dragon Ball Boot Animation (46.35MB)";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://205.196.122.234/snexuws5kfeg/vjzk5v33fu1u6ca/DBbootanimation.zip");
}if (gameboy != null){
fileName = "GameBoy Boot Animation";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://205.196.120.109/j9ye9cznxazg/oqu704icx2kxx30/Gbootanimation.zip");
}if (gamecube != null){
fileName = "GameCube Boot Animation";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://199.91.154.113/hzdvf89m0vag/r86xilqqq8mursc/GCbootanimation.zip");
}if (nexus != null){
fileName = "Nexus Boot Animation";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://forum.xda-developers.com/attachment.php?attachmentid=1162570&d=1340907701");
}if (nightmare != null){
fileName = "The Nightmare Before Christmas Boot Animation";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://199.91.154.15/75mtt4zeb4yg/co5d822mralvmn4/The+Nightmare+Before+Christmas.zip");
}if (xbox != null){
fileName = "New Xbox Boot Animation";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://205.196.121.24/n4ccp4ps8zzg/j2on8mnc7rqo2q2/NXbootanimation.zip");
}if (xbox1 != null){
fileName = "Old Xbox Boot Animation";
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://205.196.120.108/31oc4yj1njxg/un8leuns2md21fq/OXbootanimation.zip");
}
}
});
bReturnB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
根据上面显示的代码 在调用show之前,您似乎没有初始化
mProgressDialog
。
同样如上所述,
mProgressDialog
还有第二个声明需要删除。当执行onPreExecute()方法时,您的mProgressDialog为空
在代码中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
ProgressDialog mProgressDialog;
......
mProgressDialog = new ProgressDialog(BootAnimation.this);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setMessage("Downloading " + fileName);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
您可以在方法中初始化本地文件mProgressDialog,但不能在类中初始化。在调用
show
并注释或删除mProgressDialog
中onCreate
method.Wow中的mProgressDialog
的第二个声明之前,您不能初始化mProgressDialog
。我不敢相信我错过了,我仍然不知道为什么我把它放在那里。。。谢谢请继续回答,这样我就可以接受了。:)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
ProgressDialog mProgressDialog;
......
mProgressDialog = new ProgressDialog(BootAnimation.this);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setMessage("Downloading " + fileName);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}