2

Managed to get the codes error free, however upon launching, I somehow always get a null pointer exception at the line mMap = mapFrag.getMap();

Why is this so? Am I missing some imports or some steps? I am unsure if it's the SupportMapFragment or GoogleMap object that's causing the problem.

package com.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MapFragment extends SherlockMapFragment {

    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);
        SupportMapFragment mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map);
        mMap = mapFrag.getMap(); //null pointer is here
        return root;
    }
}

Edit: This is part of the implementation based on the solution given in this question here

Community
  • 1
  • 1
lyk
  • 1,578
  • 5
  • 25
  • 49
  • To be blunt, what are you trying to do here? You have a class named `MapFragment`, yet it does not have a map. Instead, your `MapFragment` is trying to access a *different* `MapFragment`, where you have no evidence that that other fragment exists yet, let alone is ready to give you a map. – CommonsWare Jan 26 '13 at 18:32
  • I am trying to implement the solution as given here: http://stackoverflow.com/questions/13721929/using-actionbarsherlock-with-the-new-supportmapfragment – lyk Jan 26 '13 at 18:37
  • "This is part of the implementation based on the solution given in this question" -- no, it is not. For example, show me *anywhere* in the accepted answer where a fragment is trying to access another fragment. – CommonsWare Jan 26 '13 at 18:44
  • I previously followed it exactly like in [here](http://stackoverflow.com/questions/14537196/google-map-android-api-v2-getmap-error) but I added the SupportMapFragment line because without it the getMap() is undefined, and I got this based on some other solutions out there. Unless you could help me with the question I posted in the link? – lyk Jan 27 '13 at 05:08

2 Answers2

2

Note that you do not need to use a custom Fragment subclass necessarily to use Maps V2. If your fragment is just purely a map, you can create the MapFragment or SupportMapFragment from the activity and configure it there.

You do not even need to create some sort of SherlockMapFragment to be able to have a map be part of an ActionBarSherlock-based app. The regular SupportMapFragment works just fine.

If you do want to have more business smarts in your fragment, and if you are using ActionBarSherlock, and the business logic in question needs to do things related to ActionBarSherlock (e.g., contribute action items to the action bar), then and only then do you need to worry about having some sort of SherlockMapFragment.

I can confirm that this gist contains a working SherlockMapFragment. Note that it goes into the com.actionbarsherlock.app package, as it needs some package-protected access to the rest of ActionBarSherlock.

You can then subclass that, such as creating a MyMapFragment:

public class MyMapFragment extends SherlockMapFragment {
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getMap() != null) {
      Log.d(getClass().getSimpleName(), "Map ready for use!");
    }
  }
}

You have to be a bit careful on the timing of calling getMap() -- too soon and it will return null. onActivityCreated() seems like a fairly safe time, though you are free to experiment.

Then, you just use MyMapFragment wherever you would have used SupportMapFragment:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.commonsware.android.mapsv2.sherlock.MyMapFragment"/>

Here is the complete project containing the above code.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for this, perhaps you are right about the timing of calling getMap(). However as I've said, without the SupportMapFragment object, I am unable to call the getMap() method on its own? – lyk Jan 27 '13 at 05:11
  • Ah nevermind, I realized I did a very stupid mistake of not extending SupportMapFragment class in SherlockMapFragment which caused the problem! Thank you for the example =) – lyk Jan 27 '13 at 05:36
  • Thank you so much for the Maps v2 Sherlock project! It helped me a lot ;) – lomza May 19 '13 at 19:06
0

There is no need of implementing an extra class such as SherlockMapFragment. You can handle the SupportMapFragment inside of the Fragment or the SherlockFragment class code. Look at this.

Community
  • 1
  • 1
unmultimedio
  • 1,224
  • 2
  • 13
  • 39