0

我已经实现了从 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");
}

这是我的异步任务…………

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)
4

1 回答 1

1

您应该开始显示ProgressDialogin onPreExecute()。看到这个帖子

于 2012-07-08T23:29:25.133 回答