The method getSystemService()
is not defined on fragments, so get the activity first using getActivity()
, e.g.:
ConnectivityManager connMgr = (ConnectivityManager) getActivity()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// fetch data
} else {
// display error
}
p.s: additianal note: if there is a potential risk that the fragment is running without being attached to any activity, check whether getActivity()
returns null
first.
Cheers!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…