0

I am new in Android developing and I work with video tutorial, he uses getMap() for initializing mMap object and I try with getMapAsync(). but when try to initializing my app crashed.

This is my initMap() code:

package com.example.sajjad.mymaps;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends AppCompatActivity {

    private static final int ERROR_DIALOG_REQUEST = 9901;
    GoogleMap mMap;
    private static final double
            TEHRAN_LAT = 35.741785,
            TEHRAN_LNG = 51.447100;




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

        if (servicesOk()) {
            initMap();

            setContentView(R.layout.activity_map);
            Toast.makeText(this,"Ready to map!", Toast.LENGTH_SHORT).show();
//             gotoLocation(TEHRAN_LAT,TEHRAN_LNG);
        } else {
            setContentView(R.layout.activity_main);
        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }


    public boolean servicesOk() {

        int isAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
        GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();

        if(isAvailable != ConnectionResult.SUCCESS) {
            if(googleAPI.isUserResolvableError(isAvailable)) {
                googleAPI.getErrorDialog(this, isAvailable,ERROR_DIALOG_REQUEST).show();
            }
            return false;
        }
        return true;
    }

    private void initMap() {
        SupportMapFragment mapFragment =
                ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
            }
        });

    }

    private void gotoLocation(double lat, double lng) {
        LatLng latLng = new LatLng(lat,lng);
        CameraUpdate update = CameraUpdateFactory.newLatLng(latLng);
        mMap.moveCamera(update);

    }


}
sajjad
  • 11
  • 2

1 Answers1

0

Can you please the paste the exception here?

Also, Make sure the following:

1 - you have added the map activity in the manifest file and have added the required permissions.

2 - you are initializing the map in the main thread.

3 - Make sure the google play services are installed on the device.

  • Don't forget to make sure you've set an API key for your map. To do so, visit https://console.developers.google.com/apis/ , create a project and enable Google Maps Android API. You will have some details about setting API key here : https://support.google.com/googleapi/answer/6158862?hl=en&authuser=0 – Lodoss Jan 29 '18 at 19:19
  • please paste the exception here – Muhammad Haris Jan 29 '18 at 19:50