2012-07-07 26 views
0

我已经实现代码从Internet下载文件,但在更新带有进度值(进度百分比)的GUI线程时出现空指针异常。在异步onProgress()方法中获取错误任务

这是我的代码。

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) 
+0

'Asynctask'是'CustomviewActivity'的一个子类吗? – 2012-07-08 23:20:16

+0

是的,Asynctask是它的子类。 – San 2012-07-09 13:13:45

回答

1

你应该开始显示ProgressDialogonPreExecute()代替。看到这个post