My Activty code snippet is which implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener
:
@Override
public void onConnected(Bundle bundle) {
getCurrentLocation();
}
public void getCurrentLocation() {
if (this.servicesConnected()) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
mCurrentLocation = mLocationClient.getLastLocation();
ArrayList<Address> addresses = null;
try {
latitude = String.valueOf(mCurrentLocation.getLatitude());
longitude = String.valueOf(mCurrentLocation.getLongitude());
addresses = (ArrayList<Address>) gCoder.
getFromLocation(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), 1);
mLocationClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
Log.wtf("Arsnal", "" + addresses.get(0).getLocality());
} else {
Toast.makeText(this, R.string.no_address_found, Toast.LENGTH_LONG).show();
}
Log.wtf("Location", "Lat " + latitude + " - lon " + longitude);
}
}
All I want is to get the String Latitude and longitude in my fragment ie
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.wtf("check","Inside OnCreate of Fragment!");
Log.wtf("check","Latitude"+((MyActivuty)getActivity()).latitude+
"Longitude"+((MyActivuty)getActivity()).longitude);
}
I am getting NPE in this line ie
Log.wtf("check","Latitude"+((MyActivuty)getActivity()).latitude+
"Longitude"+((MyActivuty)getActivity()).longitude);
because onComplete Method is called after fragment's OnCreate().HOw Can i solve this problem? Any help appreciated!Thanks