0

I've created a LocationBuddy class that is responsible for providing the location for all my other Fragments and activity that needs it. I create an instance of this class in the fragments for instance and then call the method getUpdatedLocation() to receive the location. The problem is that the getUpdatedLocation method returns null when called outside of the LocationBuddy class and I think that I've initialized everything fine.

LocationBuddy Class

public class LocationBuddy implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{
 GoogleApiClient client;
 Context context;

// Private instance variables
private Location updatedLocation;
private boolean clientConnected = false;
private boolean locationUpdated = false;
private int updateInterval;
private final static String TAG = "LocationBuddy";
private String lastUpdateTime;
private Location lastLocation;

// Create the LocationBuddy Object and initialize it with the current context
public LocationBuddy (Context c, int milliseconds)
{
    this.context = c;
    this.updateInterval = milliseconds;
    this.updatedLocation = null;
    this.lastLocation = null;
    Log.d(TAG, "LocationBuddy created with an update Interval of "+updateInterval+" milliseconds.");
}



// Google Location methods

@Override
public void onConnected(@Nullable Bundle bundle)
{
    lastLocation = LocationServices.FusedLocationApi.getLastLocation(client);
    if (lastLocation != null)
    {

        updatedLocation = lastLocation;

    }
    // Create a location request called locationRequest
    LocationRequest locationRequest = LocationRequest.create();


    // Set it's priority to high accuracy
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    // Set it to update to an interval set on object creation
    locationRequest.setInterval(updateInterval);

    // If another app is requesting location updates
    locationRequest.setFastestInterval(updateInterval);

    // Call requestLocationUpdates in the Api with this request
    LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);





}

@Override
public void onConnectionSuspended(int i)
{
    Log.d(TAG, "The Google API client has been suspended");
}

@Override
public void onLocationChanged(Location location)
{
    Log.d(TAG, "The location has been changed to " + location.toString());
    locationUpdated = true;
    lastLocation = location;
    updatedLocation = lastLocation;

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{

}

// Public Methods
public void initialize()
{

    // Build the GoogleApi Client
    client = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    connectToGoogleApiClient();

}




public void connectToGoogleApiClient()
{
    client.connect();
    clientConnected = true;
}

public void disconnectFromGoogleApiClient()
{
    client.disconnect();
    clientConnected = false;
}



public String statusReport()
{
    return "Location Buddy: Client connected: "+clientConnected+", Location updated: "+locationUpdated+", Update interval: "+updateInterval;
}

public Location getUpdatedLocation()
{
    return updatedLocation;
}

public int getUpdateInterval()
{
    return updateInterval;
}



public boolean isLocationUpdated()
{

    return locationUpdated;
}

public boolean isClientConnected()
{
    return clientConnected;
}

// Return a String description of this instance
public String toString()
{
    return "LocationBuddy[updatedLocation=" + updatedLocation + ",interval=" + updateInterval + ", clientConnected="+clientConnected+ ", locationUpdated="+locationUpdated+"]";
}

}

This is a snippet from the PackageListFragment.java class which uses this LocationBuddy object.

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    courier = new Courier(getContext());
    buddy = new LocationBuddy(getContext(), 1000);
    buddy.initialize();

}

And this is called from the OnActivityCreated method in PackageListFragment class

currentLocation = buddy.getUpdatedLocation();

And this is the error from logcat:

07-22 16:13:24.351 27538-27538/com.shipwebsource.courier E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.shipwebsource.courier, PID: 27538
                                                                       java.lang.NullPointerException
                                                                           at com.shipwebsource.courier.Extras.LocationBuddy.getUpdatedLocation(LocationBuddy.java:146)
                                                                           at com.shipwebsource.courier.PackageListFragment.onActivityCreated(PackageListFragment.java:306)
                                                                           at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1983)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                           at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                           at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                           at android.os.Handler.handleCallback(Handler.java:733)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:136)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                             at dalvik.system.NativeStart.main(Native Method)
07-22 16:13:26.119 27538-28841/com.shipwebsource.courier  D/dalvikvm:GC_FOR_ALLOC freed 1427K, 18% free 7077K/8620K, paused 8ms, total 8ms

1 Answers1

0

You never set the client variable to be anything. It is only created and never assigned to a new GoogleApiClient until you set up your other stuff (from what I can tell).

TheAnonymous010
  • 725
  • 7
  • 19