I've been trying to get a Location Manager to work for a few hours now inside my Fragment. I found a StackOverflow question about a similar problem, and tried to implement the solution. The answer is located here: https://stackoverflow.com/a/18533440/3035598
So i almost literally copied everything the answer said, but it is not working for me. When the map opens I get the error "Google Play Services Missing". This is caused by a NullPointerException as you can read in the answer.
I have no idea why it is not working, since I did everything he said.
Does anyone know what's going wrong?
If I have to provide my code, let me know and I will do that, but it is almost the same as in the link I provided.
EDIT:
The Code I use:
package com.example.bt6_aedapp;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
public class fragmentB extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
private GoogleMap map;
private LatLng latlng;
private LocationRequest lr;
private LocationClient lc;
MapFragment mapFragment;
ImageView iv;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if(parent != null) {
parent.removeView(view);
}
}
try {
view = inflater.inflate(R.layout.fragment_b, container, false);
mapFragment = ((MapFragment) this.getActivity().getFragmentManager().findFragmentById(R.id.map));
iv = (ImageView) view.findViewById(R.id.iv);
map = mapFragment.getMap();
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(false);
MapsInitializer.initialize(this.getActivity());
}
catch (InflateException e) {
Toast.makeText(getActivity(), "Problems inflating the view !", Toast.LENGTH_LONG).show();
}
catch (NullPointerException e) {
Toast.makeText(getActivity(), "Google Play Services missing !", Toast.LENGTH_LONG).show();
}
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lr = LocationRequest.create();
lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
lc = new LocationClient(this.getActivity().getApplicationContext(),
this, this);
lc.connect();
}
@Override
public void onLocationChanged(Location location) {
latlng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latlng, 10);
map.animateCamera(cameraUpdate);
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
}
@Override
public void onConnected(Bundle connectionHint) {
lc.requestLocationUpdates(lr, this);
}
@Override
public void onDisconnected() {
}
}
The error I'm getting now is located at row 115: java.lang.NullPointerException at com.example.bt6_aedapp.fragmentB.onLocationChanged(fragmentB.java:155)
I checked the location.getLatitude() and location.getLongitude() and both of them are NOT empty, they return a correct value.