2013-03-30 48 views
0

對不起,這樣的問題,但我一直在努力解決它幾個小時,它不會工作。我正在使用AsyncTask下載文件並顯示進度。Android 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(); 
     } 
    }); 

    } 
} 

我想,也許這是使得它崩潰的按鈕,我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) 
+3

在'onCreate'方法內調用'show'and註釋或刪除'mProgressDialog'的第二個聲明之前,您沒有初始化'mProgressDialog'。 –

+1

哇。我不能相信我錯過了,我仍然不知道爲什麼我把它放在那裏......謝謝!繼續,並將其作爲答案,以便我可以接受它。 :) – user2182912

回答

1

從上面所示的代碼,

看來你沒有初始化mProgressDialog在調用表演。 也如上所述,還有第二個聲明mProgressDialog也需要刪除。

0

執行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但類。