0

I am getting a java.lang.NullPointerException on the line map.setOnMyLocationChangeListener(myLocationChangeListener);. Strangely, this exception arises while running it some devices while in some it doesn't. How do I solve this?

public class MyMap extends ActionBarActivity implements
    RoutingListener, OnMapReadyCallback,
     SensorEventListener {

// private GoogleApiClient mGoogleApiClient;
protected GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_map);

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

    if (mapFragment == null) {
        mapFragment = SupportMapFragment.newInstance();
        getSupportFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
    }

    mapFragment.getMapAsync(this);
    map = mapFragment.getMap();


    map.setOnMyLocationChangeListener(myLocationChangeListener);
}

EDIT:

@Override
public void onMapReady(GoogleMap mMap) {
    map = mMap;

    mUiSettings = map.getUiSettings();
    mUiSettings.setZoomControlsEnabled(true);
    mUiSettings.setCompassEnabled(true);

    updateMyLocation();


}
4
  • extends FragmentActivity First Commented Aug 7, 2015 at 6:05
  • Can you explain a bit more on why and how does it solve the problem? Commented Aug 7, 2015 at 6:07
  • 2
    ActionBarActivity already extends FragmentActivity @IntelliJ Amiya Commented Aug 7, 2015 at 6:19
  • Yes ,Mind it ActionBarActivity is deprecated .I here i used Appcompat instead ABA Commented Aug 7, 2015 at 6:24

2 Answers 2

1

By calling mapFragment.getMapAsync(this);, I suppose you have to override this method public void onMapReady(GoogleMap googleMap). Set your mMap = googleMap there and set all other listener there. Keep in mind that your Map object needs some time to be initialised, that's why they give you getMapAsync method.

Editted:

Please remove this from your onCreated

map = mapFragment.getMap(); map.setOnMyLocationChangeListener(myLocationChangeListener);

and put them map.setOnMyLocationChangeListener(myLocationChangeListener); inside your onMapReady() after this line map = mMap. Please acknowledge when your map is initialized.

Sign up to request clarification or add additional context in comments.

4 Comments

I am already overriding onMapReady. I have included it in the EDIT.
Well, REMOVE this map = mapFragment.getMap(); map.setOnMyLocationChangeListener(myLocationChangeListener); from your onCreated. As I said, your map is still null at this moment.
@Nguyen : So should I set my map to mapFragment.getMap(); or GoogleMap mMap?
Oh my bad, map = mMap; map.setOnMy..... Don't need to call your fragment anymore
0

When you work with GoogleMap then it takes time to initialize. Sometimes it takes longer time in some devices than others.

To solve this issue, you should initialize the GoogleMap object with some delay. You can follow the below strategy:

private Handler handler = new Handler();

private Runnable mapChecker = new Runnable() {

    @Override
    public void run() {
        try {

            if (mapFragment.getMap() != null) {

                map = mapFragment.getMap();
                map.setOnMyLocationChangeListener(myLocationChangeListener);

                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        handler.postDelayed(mapChecker, 500);
    }
};

Then from onCreate(),

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_map);

    ..........

    handler.postDelayed(mapChecker, 500);

    ..........

}

2 Comments

Since new API have getMapAsync method and onMapReady callback method, you don't need this anymore. Take a look at my Answer.
since lately I didn't with GoogleMap, I was unaware of this callback. Now, I see this...its good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.