0

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.

Community
  • 1
  • 1
Kevin Sleegers
  • 475
  • 2
  • 5
  • 19
  • check that, google play service is installed in your device. – maddy d Mar 24 '14 at 10:29
  • In my device? I'm using a Nexus 5 (Android 4.4). How would I do that? – Kevin Sleegers Mar 24 '14 at 10:30
  • Have you added **Google play services** as library inside your project? – Naddy Mar 24 '14 at 10:36
  • @Naddy, Yes I have. I've included it as a library and set it as library in my Project Properties. – Kevin Sleegers Mar 24 '14 at 10:37
  • ""Google Play Services Missing" because [Google Play services](https://play.google.com/store/apps/details?id=com.google.android.gms) is not installed. – maddy d Mar 24 '14 at 10:38
  • try updating the google play services . if present, on your device. If not, try installing the google play services. Also, open the google maps once and click Accept Link. Update every application related to google once. Sometimes, it creates problems like these – Atish Agrawal Mar 24 '14 at 10:41
  • All my apps are up-to-date. I've edited my question because I think something else might be wrong. – Kevin Sleegers Mar 24 '14 at 10:44
  • Wait a second, now my application seems to give me a more specific error in the logcat: `The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.` The google-play-services-lib.jar is included in the Android Dependencies. – Kevin Sleegers Mar 24 '14 at 10:45

1 Answers1

1

Alright, so after a lot of debugging and research, I've found the solution.

All I had to do was replace

`mapFragment = ((MapFragment) this.getActivity().getFragmentManager().findFragmentById(R.id.map));`

with:

mapFragment = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map));

Kevin Sleegers
  • 475
  • 2
  • 5
  • 19