簡體   English   中英

在異步任務的onProgress()方法中獲取錯誤

[英]Getting Error in onProgress() method of Async Task

我已經實現了從Internet下載文件的代碼,但是在用進度值(進度百分比)更新我的GUI線程時出現了空指針異常。

這是我的代碼。

 public class CustomviewActivity extends Activity {
ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


 // instantiate it within the onCreate method
 mProgressDialog = new ProgressDialog(CustomviewActivity.this);
 mProgressDialog.setMessage("File Downloading Start....");
 mProgressDialog.setIndeterminate(false);
 mProgressDialog.setMax(100);
 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

 // execute this when the downloader must be fired
 mProgressDialog.show();
 Asynctask downloadFile = new Asynctask();
 downloadFile.execute("http://sound21.mp3pk.com/indian/jodibreakers/jodi-breakers03(www.songs.pk).mp3");
}

這是我的Asynctask ......

public class Asynctask extends AsyncTask<String, Integer, String> {
       @Override
        protected String doInBackground(String... sUrl) {
            try {
                URL url = new URL(sUrl[0]);
                URLConnection connection = url.openConnection();
                connection.connect();
                // this will be useful so that you can show a typical 0-100% progress bar
                int fileLength = connection.getContentLength();

                // download the file
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/file_name.mp3");

                byte data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing 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 onProgressUpdate(Integer... values) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(values);
     mProgressDialog.setProgress(values[0]);
}
@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}

}

出現錯誤后,我進入LogCat

07-07 22:30:38.496: ERROR/AndroidRuntime(22625): FATAL EXCEPTION: main
07-07 22:30:38.496: ERROR/AndroidRuntime(22625): java.lang.NullPointerException
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.custom.CustomviewActivity$Asynctask.onProgressUpdate(CustomviewActivity.java:103)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.custom.CustomviewActivity$Asynctask.onProgressUpdate(CustomviewActivity.java:1)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.os.Looper.loop(Looper.java:150)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at android.app.ActivityThread.main(ActivityThread.java:4389)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at java.lang.reflect.Method.invoke(Method.java:507)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at dalvik.system.NativeStart.main(Native Method)

您應該開始在onPreExecute()顯示ProgressDialog 看到這篇文章

暫無
暫無

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

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