我的代码检查互联网连接。
public boolean internet() {
boolean flag = false;
//context=FindPeopleFragment_revice.this;
// FindPeopleFragment_revice.context = this;
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo[] info = cm.getAllNetworkInfo();
if (info != null) {
if (info[0].getState() == NetworkInfo.State.CONNECTED
|| info[1].getState() == NetworkInfo.State.CONNECTED) {
flag = true;
} else {
flag = false;
}
}
}
return flag;
}
当我关闭/开启位置API 例外时出现:
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
Ecexption起来:
08-25 12:47:25.184: E/AndroidRuntime(29169): FATAL EXCEPTION: main
08-25 12:47:25.184: E/AndroidRuntime(29169): Process: info.staffmark, PID: 29169
08-25 12:47:25.184: E/AndroidRuntime(29169): java.lang.NullPointerException
08-25 12:47:25.184: E/AndroidRuntime(29169): at info.staffmark.FindPeopleFragment_revice.internet(FindPeopleFragment_revice.java:1210)
08-25 12:47:25.184: E/AndroidRuntime(29169): at info.staffmark.FindPeopleFragment_revice.onLocationChanged(FindPeopleFragment_revice.java:1822)
08-25 12:47:25.184: E/AndroidRuntime(29169): at com.google.android.gms.internal.jg$a.handleMessage(Unknown Source)
08-25 12:47:25.184: E/AndroidRuntime(29169): at android.os.Handler.dispatchMessage(Handler.java:102)
08-25 12:47:25.184: E/AndroidRuntime(29169): at android.os.Looper.loop(Looper.java:146)
08-25 12:47:25.184: E/AndroidRuntime(29169): at android.app.ActivityThread.main(ActivityThread.java:5511)
08-25 12:47:25.184: E/AndroidRuntime(29169): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 12:47:25.184: E/AndroidRuntime(29169): at java.lang.reflect.Method.invoke(Method.java:515)
08-25 12:47:25.184: E/AndroidRuntime(29169): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
08-25 12:47:25.184: E/AndroidRuntime(29169): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
08-25 12:47:25.184: E/AndroidRuntime(29169): at dalvik.system.NativeStart.main(Native Method)
OnLocation Change方法:
@Override
public void onLocationChanged(android.location.Location location) {
// TODO Auto-generated method stub
// internet = internet();
internet = cd.isConnectingToInternet();
Log.d("Net Status is", "Net is " + internet);
if (internet) {
Double Latitude=location.getLatitude();
Double Longitude=location.getLongitude();
variable.CurrentLat=Latitude;
variable.CurrentLong=Longitude;
Log.d("Latitude is", ""+Latitude);
Log.d("Longitude is", ""+Longitude);
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final Geocoder gcd = new Geocoder(getActivity().getApplicationContext());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(Latitude, Longitude, 10);
for (Address address : addresses) {
if(address.getPostalCode()!=null){
// Log.d("licality",address.getLocality());
Log.d("postalcode","Postal Code is"+address.getPostalCode());
variable.currentZip=address.getPostalCode();
internet = internet();
if (internet) {
if(variable.LocationType.equals("")){
internet=internet();
if(internet){
if(LocationType.equals("")){
Location();
}
}else{
Toast.makeText(getActivity(), "Internet service is not available in your device.", Toast.LENGTH_LONG).show();
}
}else{
internet=internet();
if(internet()){
Data();
ShowMarkerMap();
}else{
Toast.makeText(getActivity(), "Internet service is not available in your device.", Toast.LENGTH_LONG).show();
}}
} else {
Toast.makeText(getActivity(),"Internet service is not available in your device.",Toast.LENGTH_LONG).show();
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
检查互联网连接时,在On位置更改方法出现异常。
我很困惑如何解决这个问题。
请帮我解决这个问题,谢谢。
答案 0 :(得分:0)
如果此代码位于片段的oncreate()中,请尝试将其放入onActivityCreated()中。 我的猜测:getActivity返回null。 你应该至少把你的堆栈跟踪放在你的问题中。
检查:http://developer.android.com/reference/android/app/Fragment.html#onCreate(android.os.Bundle)
它告诉你: &#34;请注意,在片段的活动仍处于创建过程中时,可以调用此方法。因此,您不能依赖此时正在初始化的活动的内容视图层次结构等内容。如果您想在创建活动本身后继续工作,请参阅onActivityCreated(Bundle)。&#34;