2012-01-25 28 views
1

收到此錯誤:的AsyncTask doInBackground返回一個NullPointerException只是有時

java.lang.RuntimeException: An error occured while executing doInBackground() 
at android.os.AsyncTask$3.done(AsyncTask.java:200) 
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 
at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 
at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
at java.lang.Thread.run(Thread.java:1027) 
Caused by: java.lang.NullPointerException 
at com.Wahoo.BrowseListActivity$DownloadSite.doInBackground(BrowseListActivity.java:79) 
at com.Wahoo.BrowseListActivity$DownloadSite.doInBackground(BrowseListActivity.java:1) 
at android.os.AsyncTask$2.call(AsyncTask.java:185) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
... 4 more 

這裏是我的AsyncTask ......它崩潰僅在特定時間一些用戶......不知道爲什麼。也許他們在查詢中失去了他們的互聯網連接?我在這裏做錯了什麼?這裏是我的代碼:

private class DownloadSite extends AsyncTask<String, Integer, String> { 
     private HttpResponse response; 
     private InputStream in; 
     private Context context; 
     private String html; 
     private ProgressDialog progress; 


     @Override 
     protected String doInBackground(String... params) { 

      in = null; 

      String url = "aURLGOESHERE_BUTI'MCENSORING"         + params[0] + ""; 

      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(url); 
      HttpResponse response = null; 

      try { 
       response = client.execute(request); 
      } catch (ClientProtocolException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

      try { 
       in = response.getEntity().getContent(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (Exception e) { 

       e.printStackTrace(); 

      } 
      html = null; 

      BufferedReader reader = null; 
      try { 
       reader = new BufferedReader(new InputStreamReader(in)); 
      } catch (Exception e) { 

       this.publishProgress(); 
       this.cancel(true); 

       e.printStackTrace(); 
      } 

      StringBuilder str = new StringBuilder(); 
      String line = null; 

      try { 
       while ((line = reader.readLine()) != null) { 
        str.append(line); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       in.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      html = params[0] + str.toString(); 

      return html; 

     } 

     @Override 
     protected void onPreExecute() { 

      progress = new ProgressDialog(BrowseListActivity.this); 
      progress.setIndeterminate(true); 
      progress.setMessage("Loading..."); 
      progress.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      progress.show(); 
     } 

     @Override 
     protected void onProgressUpdate(Integer... progress) { 

      CharSequence text = "Connection interrupted...please try again"; 
      int duration = Toast.LENGTH_LONG; 
      Toast toast = Toast.makeText(getApplicationContext(), text, 
        duration); 
      toast.show(); 
     } 

     @Override 
     protected void onPostExecute(String html) { 
      progress.dismiss(); 

      Context context = BrowseListActivity.this; 
      Intent stopViewer = new Intent(context, StopActivity.class); 
      stopViewer.setData(Uri.parse(html + "")); 
      context.startActivity(stopViewer); 

     } 

    } 
+0

哪種類似的代碼是79行? –

回答

3

一件事,你做錯了繼續,使得它不可能有意義繼續錯誤後執行doInBackground。例如:

try { 
    response = client.execute(request); 
} catch (ClientProtocolException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 

如果拋出一個異常,response將是null並有進一步繼續進行沒有意義的。您將在下一個代碼塊中生成NullPointerException。這不會是致命的,因爲你在那裏捕捉所有的例外。然而,進一步說,這種模式會重複出現,並且你沒有捕捉到所有的例外。

您應該過早退出,並返回null作爲字符串結果。然後,您可以測試onPostExecute中的null,並讓用戶以優雅的方式知道發生了什麼。

+0

那是對的 - 任何HTTP調用都應該包含空響應。您可以在'doInBackground()'中執行空檢查,但如果您返回null並根據具體情況在'onProgressUpdate()'或'onPostExecute()'中執行空檢查,那麼它確實保持乾淨。 – curioustechizen

+0

太棒了!謝謝,我會研究這個,並接受這個迴應,如果我發現這個工作。感謝您的好評。 – Rohan

相關問題