有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在异步任务的onProgress()方法中获取错误

我已经实现了从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 安卓.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at 安卓.os.Handler.dispatchMessage(Handler.java:99)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at 安卓.os.Looper.loop(Looper.java:150)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-07 22:30:38.496: ERROR/AndroidRuntime(22625):     at dalvik.system.NativeStart.main(Native Method)

共 (1) 个答案

  1. # 1 楼答案

    您应该开始在onPreExecute()中显示ProgressDialog。看这个post