0

I have spent a while trying to get this to work and I keep getting stuck somewhere at every tutorial. In short I am trying to make a tabbed app where one of the tabs it a google maps.

I have fixed all of the usual mistakes:

I have downloaded everything relevant through SDK.
I have an API key in place.
I have added compile com.google.android.gms:play-services:7.5.0 to my dependencies.

I am trying to follow this code, but I keep getting an error.

Error I am receiving from logcat:

07-06 13:46:20.046  17948-17948/dolphin.dolphinapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: dolphin.dolphinapp, PID: 17948
    java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
            at com.google.android.gms.common.internal.zzu.zzb(Unknown Source)
            at com.google.android.gms.maps.model.BitmapDescriptorFactory.zzvH(Unknown Source)
            at com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(Unknown Source)
            at dolphin.dolphinapp.MainActivity$MapFragment.onCreateView(MainActivity.java:655)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
            at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:494)
            at dolphin.dolphinapp.MainActivity.onTabSelected(MainActivity.java:152)
            at android.support.v7.internal.app.WindowDecorActionBar.selectTab(WindowDecorActionBar.java:640)
            at android.support.v7.internal.app.WindowDecorActionBar$TabImpl.select(WindowDecorActionBar.java:1224)
            at android.support.v7.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:568)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Apparently to solve this write MapsInitializer.initialize(getActivity().getApplicationContext());, but that is already in the code I copied.

Here is my Java Code:

public class MapFragment extends Fragment {

        MapView mMapView;
        private GoogleMap googleMap;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // inflat and return the layout
            View v = inflater.inflate(R.layout.fragment_location_info, container,
                    false);

            mMapView = (MapView) v.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);

            mMapView.onResume();// needed to get the map to display immediately

            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }

            MapsInitializer.initialize(getApplicationContext());

            googleMap = mMapView.getMap();
            // latitude and longitude
            double latitude = 17.385044;
            double longitude = 78.486671;


            // create marker
            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

            // Changing marker icon
            marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

            // adding marker
            googleMap.addMarker(marker);

            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();

            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            // Perform any camera updates here
            return v;
        }

        @Override
        public void onResume() {
            super.onResume();
            mMapView.onResume();
        }

        @Override
        public void onPause() {
            super.onPause();
            mMapView.onPause();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mMapView.onDestroy();
        }

        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mMapView.onLowMemory();
        }
    }

My XML File:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Please let me know what I am doing wrong

Community
  • 1
  • 1
JohnF
  • 23
  • 8
  • 1
    If there's nothing other than the map in your fragment, why not use [MapFragment](https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment) out of the box? – SqueezyMo Jul 06 '15 at 18:05
  • Sorry but I am really new to this. How exactly would I do this? – JohnF Jul 06 '15 at 18:08
  • @AlecS you should read and understand documentation before you write code https://developers.google.com/maps/documentation/android/map – tyczj Jul 06 '15 at 18:11
  • @AlecS You can simply extend from MapFragment. In that case you don't even have to override lifecycle callbacks to get some basic functionality. Like tyczj said, try to find some good tutorial on the subject. – SqueezyMo Jul 06 '15 at 18:26

1 Answers1

2

you should call mMapView.getMapAsync() in your onCreateView then in the callback of onMapReady that you implement you would do MapsInitializer.initialize(this.context);

mMapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
           MapsInitializer.initialize(getApplicationContext());
        // latitude and longitude
        double latitude = 17.385044;
        double longitude = 78.486671;


        // create marker
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));             
    }
});
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • ok. I put that in my onCreateView. And I got `getMapAsync(com.google.androuid.gms.maps.OnMapReadyCallBack) in 'com.google.android.gms.maos.MapView' cannot be applied to '()'`. Where is `onMapReady`? is something I need to make? – JohnF Jul 06 '15 at 18:14
  • you need to implement the callback like i said – tyczj Jul 06 '15 at 18:15
  • I now have this `public class MapFragment extends Fragment implements OnMapReadyCallback{`, but I am getting the same error. – JohnF Jul 06 '15 at 18:32
  • Now I am getting an error Class 'MapFragment" must either be declared abstract or implement abstract method 'onMapReady(GoogleMap)' in 'OnMapReadyCallback' – JohnF Jul 06 '15 at 18:48
  • again you need to implement the `OnMapReady` method in your code! – tyczj Jul 06 '15 at 18:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82531/discussion-between-alec-s-and-tyczj). – JohnF Jul 06 '15 at 19:04