2012-10-25 70 views
2

Fragmet中CONNECTIVITY_SERVICE的上下文是什麼,我也檢查過getActivity,但它給出錯誤。Fragment中CONNECTIVITY_SERVICE的上下文是什麼?

public boolean isOnline() { 
      ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
      try { 
       if (connectionManager.getActiveNetworkInfo().isConnected()) { 
        Log.d("ConStatus", "Data Connection On"); 
        return true; 
       } else { 
        Log.d("ConStatus", "Data Connection off"); 
        return false; 
       } 
      } catch (NullPointerException e) { 
       Log.i("ConStatus", "No Active Connection"); 
       return false; 
      } 
     } 
+0

你能提供的堆棧跟蹤,當你使用getActivity? – Darwind

+0

我寫了getSystemService(getActivity()。CONNECTIVITY_SERVICE);並在那裏它自我給出一個錯誤,請創建一個方法getSystemService。 – user1744952

回答

3

getSystemService()Context上的方法。一個Fragment將使用getActivity()稱之爲:

getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 
+0

即時通訊使用barsherlock Fragements,但它崩潰。在我的情況下給我Nullpointer異常 –