0

I have an activity where I just want to display a map. Thus, I use a MapView along with a GoogleMap object to do so. However, I got a null object error in my Maps activity when trying to change the map type of my map. I have read some topics on such a problem but my problem persists.

public class Maps extends AppCompatActivity {


        private GoogleMap map;
        private MapView map_view


        static final LatLng HAMBURG = new LatLng(53.558, 9.927);
        static final LatLng KIEL = new LatLng(53.551, 9.993);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.maps);
            Log.i("DEBUG", "Dans Maps");

            // MapView to display the further display clicked place
            map_view = (MapView)findViewById(R.id.mapview);
            map_view.onCreate(savedInstanceState);


            // Gets to GoogleMap from the MapView and does initialization stuff
            map_view.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    map = googleMap;
                }
            });

            map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
            map.getUiSettings().setMyLocationButtonEnabled(false);
            map.setMyLocationEnabled(true);
        }





        @Override
        protected void onDestroy() {
            super.onDestroy();
            map_view.onDestroy();
        }
        @Override
        protected void onResume() {
            super.onResume();
            map_view.onResume();
        }
        @Override
        protected void onPause() {
            super.onPause();
            map_view.onPause();
        }
        @Override
        public void onLowMemory() {
            super.onLowMemory();
            map_view.onLowMemory();
        }
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            map_view.onSaveInstanceState(outState);
        }

        @Override
        protected void onStart() {
            super.onStart();
            //mGoogleApiClient.connect();

        }

        @Override
        protected void onStop() {
            //mGoogleApiClient.disconnect();
            super.onStop();

        }


    }

Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="nicolas.florian.appliflo">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.access_network_state" />
    <uses-permission android:name="com.tp.lib.tp.permission.MAPS_RECEIVE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.access_coarse_location" />
    <uses-permission android:name="android.permission.access_fine_location" />
    <uses-permission android:name="android.permission.write_external_storage" />



    <application


        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name=".Welcome"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ConnectUser"
            android:label="connectUser">
        </activity>
        <activity
            android:name=".UsersListActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".AddNewUser"
            android:label="@string/title_activity_add_new_user" >
        </activity>
        <activity
            android:name=".AddPresent"
            android:label="AddPresent" >

        </activity>

        <activity
            android:name=".WebBrowserToRetrieveURL"
            android:label="WebBrowserToRetrieveURL" >

        </activity>

        <activity
            android:name=".AfterConnection"
            android:label="MainActivity" >

        </activity>
        <activity
            android:name=".PresentList"
            android:label="PresentList" >

        </activity>

        <activity
            android:name=".Maps"
            android:label="Maps" >

        </activity>






        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="MY_KEY"/>

        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"
            />

    </application>

</manifest>

And the layout file, associated with my Maps activity:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">


        <com.google.android.gms.maps.MapView
            android:id="@+id/mapview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/myLocation"
            android:enabled="true"
            android:clickable="true"
            />

        <Button
            android:id="@+id/button_select_location"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/mapview"/>




    </LinearLayout>


</RelativeLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
floflo29
  • 2,261
  • 2
  • 22
  • 45

1 Answers1

2

map is never initialized until onMapReady, therefore all other usages of it in onCreate will cause a NullPointerException.

You can try this

map_view.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);
    }
});
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Is there a true advantage to use a getMapAsync rather than a classic getMap()? – floflo29 Sep 12 '16 at 19:57
  • Asynchronous code puts less stress on the UI thread? – OneCricketeer Sep 12 '16 at 19:58
  • Ok. So, regarding the GoogleMap object "map", whatever the function that needs "map", it should be moved to the getMapAsync(), if I understand correctly? – floflo29 Sep 12 '16 at 20:09
  • You can define as many methods as you want that accept a `GoogleMap map` parameter, yes. I might suggest having the Activity implement the `OnMapReadyCallback` to make the code a little cleaner – OneCricketeer Sep 12 '16 at 20:11