2013-02-19 43 views
0

我得到null當用戶中斷傳輸到數據饋送時,強制關閉我的Android應用程序。或者有時候如果互聯網是緩慢的,它似乎搪塞我得到null當數據傳輸INTERupted時,適配器中的空指針

這是我如何處理HTTP代碼:

 HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url_select); 

     try { 
      httpPost.setEntity(new UrlEncodedFormEntity(param)); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 

      // read content 
      is = httpEntity.getContent(); 

     } catch (Exception e) { 

      Log.e("log_tag", "Error in http connection " + e.toString()); 
     } 
     try { 
      BufferedReader br = new BufferedReader(
        new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 
      String line = ""; 
      while ((line = br.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 

     } catch (Exception e) { 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     return null; 
    } 
AsyncTask,這裏返回null的 onPostExecute

然後如所見下面的logcat:

adapter = new ReviewAdapter(getActivity(), r, tf); // Line 1164 

我認爲r是什麼null殺死它? (tf是TrueType字體我通過)

這裏是r代碼:

 for (String i : reviews) { 
      r[index] = new Review(reviews.get(index), 
        datelist.get(index), userlist.get(index), 
        itemlist.get(index), ratings.get(index), 
        helpfulCount.get(index), chatCounts.get(index), 
        catlist.get(index), titlelist.get(index)); 
      index++; 
     } 

這些ArrayLists通過JSON分析從PHP填充等

所以我的問題是:如何做我處理這個普遍?是否有避免空?或者我該如何優雅地拋出錯誤而不是強制關閉?

這裏是logcat的:

02-19 12:15:12.012: E/AndroidRuntime(32116): FATAL EXCEPTION: main 
02-19 12:15:12.012: E/AndroidRuntime(32116): java.lang.NullPointerException 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.---.---.ReviewAdapter.<init>(ReviewAdapter.java:42) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1164) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.---.---.RateFragmentActivity$ReviewFragment$ReviewTask.onPostExecute(RateFragmentActivity.java:1) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.AsyncTask.finish(AsyncTask.java:631) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.AsyncTask.access$600(AsyncTask.java:177) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.os.Looper.loop(Looper.java:137) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at android.app.ActivityThread.main(ActivityThread.java:5193) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at java.lang.reflect.Method.invoke(Method.java:511) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 
02-19 12:15:12.012: E/AndroidRuntime(32116): at dalvik.system.NativeStart.main(Native Method) 

回答

1

如果第一try塊拋出一個異常,你應該只是停在那裏,而不是與第二try塊打擾,因爲is將有可能無法httpEntity.getContent();如果httpClient.execute的價值拋出一個異常(它會在連接不良的情況下執行)

爲什麼你不能檢查r是否爲null,並向用戶指出錯誤,指出它們的連接n丟失了?

+0

什麼是用'r'檢查null的最佳方法?我試過如果(r == null),然後黯然失色說我有'死代碼'。 – KickingLettuce 2013-02-19 20:49:33

+1

這表明'r'永遠不會爲空,可能是因爲您將它設置爲在檢查之前,例如'r = new Object [5]'沒有看到'r'在哪裏被初始化(未在代碼中顯示)我不能再說了。 – Dororo 2013-02-19 21:42:37

+0

你是對的,這是一個初始化問題。 – KickingLettuce 2013-02-19 21:46:32