-1

My Code to check internet connection.

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;
}

When I Turn off/On Location API at that time Exception Arise at a point:

ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);

Ecexption Arise:

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 method:

@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();
        }


     }

    }

Exception arise at On location change method while checking internet connection.

I confuse how to solve this.

Please help me to solve this, Thanks.

user3916943
  • 61
  • 2
  • 9

1 Answers1

0

in case this code is in the oncreate() of your fragment, try putting it in onActivityCreated(). My guess: getActivity returns null. You should atleast put your stacktrace in your question thought.

Check : http://developer.android.com/reference/android/app/Fragment.html#onCreate(android.os.Bundle)

It tells you: "Note that this can be called while the fragment's activity is still in the process of being created. As such, you can not rely on things like the activity's content view hierarchy being initialized at this point. If you want to do work once the activity itself is created, see onActivityCreated(Bundle)."

Wagner Michael
  • 2,172
  • 1
  • 15
  • 29