簡體   English   中英

Android AsyncTask總是崩潰

[英]Android AsyncTask always crashes

很抱歉遇到這樣的問題,但是我已經嘗試修復了幾個小時,並且無法正常工作。 我正在使用AsyncTask下載文件並顯示進度。

我曾經在我的應用程序中使用過它,並且效果很好,所以我只是將代碼復制到另一個類中並進行了一些編輯。 但是無論我做什么,當我單擊下載按鈕時,它都會崩潰。

我確實具有Internet權限,以及寫入外部存儲的權限。 這是代碼:

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();
        }
    });

    }
}

我以為可能是導致它崩潰的按鈕,並且我的AsyncTask很好,但是如果說if(android != null){ ,我更改了圖像,然后在測試時更改了圖像精細。 因此,與按鈕或字符串沒有任何關系,一切正常。

它必須與我的AsyncTask做一些事情。 這與上一類中的AsyncTask基本相同,效果很好,所以我不確定問題出在哪里。

這是一條日志:

E/AndroidRuntime(24151): FATAL EXCEPTION: main
E/AndroidRuntime(24151): java.lang.NullPointerException
E/AndroidRuntime(24151):    at com.cydeon.plasmamodz.BootAnimation$DownloadFile.onPreExecute(BootAnimation.java:76)
E/AndroidRuntime(24151):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)  
E/AndroidRuntime(24151):    at android.os.AsyncTask.execute(AsyncTask.java:534)
E/AndroidRuntime(24151):    at com.cydeon.plasmamodz.BootAnimation$1.onClick(BootAnimation.java:189)
E/AndroidRuntime(24151):    at android.view.View.performClick(View.java:4204)
E/AndroidRuntime(24151):    at android.view.View$PerformClick.run(View.java:17355)
E/AndroidRuntime(24151):    at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(24151):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(24151):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(24151):    at  android.app.ActivityThread.main(ActivityThread.java:5235)
E/AndroidRuntime(24151):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24151):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(24151):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
E/AndroidRuntime(24151):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
E/AndroidRuntime(24151):    at dalvik.system.NativeStart.main(Native Method)

根據上面顯示的代碼,

看來您沒有在調用show之前初始化mProgressDialog 並且mProgressDialog ,第二個mProgressDialog聲明也需要刪除。

當執行onPreExecute()方法時,您的mProgressDialog為null。

在您的代碼中:

@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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM