Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference MapView in SupportMapFragment

I have a Fragment which Extends SupportMapFragment.

public class PlaceMapsFragment extends SupportMapFragment {
    private GoogleMap mMap;
    private LatLng mPosFija;

    public PlaceMapsFragment() {
        super();
    }

    public static PlaceMapsFragment newInstance(LatLng posicion) {
        PlaceMapsFragment frag = new PlaceMapsFragment();
        frag.mPosFija = posicion;
        return frag;
    }

    @Override
    public GoogleMap getMap() {
        // TODO Auto-generated method stub
        return super.getMap();
    }

    @Override
    public void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        //initMap();
        return view;
    }

    private void initMap() {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija, 16));
        getMap().addMarker(
                new MarkerOptions().position(mPosFija)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.marker)));
    }

}

Now i want to refernce the MapView that is automatically generated so that i can add my functionality to the map.

When i try to initMap(); i get NullPointerException

Error:

12-10 02:54:20.171: E/AndroidRuntime(4339): FATAL EXCEPTION: main
12-10 02:54:20.171: E/AndroidRuntime(4339): java.lang.NullPointerException
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.ar.b.a(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.y.h.a(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.y.au.a(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at maps.y.ae.moveCamera(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:83)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Binder.transact(Binder.java:297)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.m7.nomad.PlaceMapsFragment.initMap(PlaceMapsFragment.java:55)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.m7.nomad.PlaceMapsFragment.onCreateView(PlaceMapsFragment.java:46)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.m7.nomad.PlaceActivity$TabManager.onTabChanged(PlaceActivity.java:153)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:379)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabHost.setCurrentTab(TabHost.java:364)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.view.View.performClick(View.java:3591)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.view.View$PerformClick.run(View.java:14263)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Handler.handleCallback(Handler.java:605)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.os.Looper.loop(Looper.java:137)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at android.app.ActivityThread.main(ActivityThread.java:4507)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at java.lang.reflect.Method.invoke(Method.java:511)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-10 02:54:20.171: E/AndroidRuntime(4339):     at dalvik.system.NativeStart.main(Native Method)
like image 733
Harsha M V Avatar asked Dec 09 '12 21:12

Harsha M V


People also ask

What is a MapView?

A View which displays a map (with data obtained from the Google Maps service). When focused, it will capture keypresses and touch gestures to move the map.

What is SupportMapFragment?

public class SupportMapFragment extends Fragment. A Map component in an app. This fragment is the simplest way to place a map in an application. It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.

Which method is called when the map is ready to use?

Callback interface for when the map is ready to be used. Once an instance of this interface is set on a MapFragment or MapView object, the onMapReady(GoogleMap) method is triggered when the map is ready to be used and provides a non-null instance of GoogleMap .


2 Answers

This is an expected behabiour.

You can call getMap() only after the Fragment has gone through onCreateView(), otherwise it will give you a null. You can also receive a null if the Google Play Services is not available.

In other words, call initMap() in onResume().

You can check more here.

Hope it helps!

like image 65
Tudor Luca Avatar answered Nov 03 '22 00:11

Tudor Luca


I finally solved it, you dont have to override getMap() and you dont need the mMap variable, the GoogleMap is included in the class. Finally you need to call initMap() in onResume:

public class PlaceMapsFragment extends SupportMapFragment {
    private LatLng mPosFija = new LatLng(37.878901,-4.779396);

    public PlaceMapsFragment() {
        super();
    }

    public static PlaceMapsFragment newInstance(LatLng posicion) {
        PlaceMapsFragment frag = new PlaceMapsFragment();
        frag.mPosFija = posicion;
        return frag;
    }

   @Override
   public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    initMap();
   }



    @Override
    public void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        //initMap();
        return view;
    }

    private void initMap() {
        UiSettings settings = getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);
        settings.setMyLocationButtonEnabled(false);

        getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija, 16));
        getMap().addMarker(
                new MarkerOptions().position(mPosFija)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.marker)));
    }

}

Hope it helps other people that are starting with the v2 of google maps api (like me :D).

like image 31
ƒernando Valle Avatar answered Nov 02 '22 23:11

ƒernando Valle