App crash when using getMapAsync() in MapsView

2019-09-02 23:08发布

I have a mapsview and it works fine when using getMap() although it shows me a depricated warning. But when i change getMap() to getMapAsync() the app will crash.

Here's my code

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMapAsync(this);

        mMap.setOnInfoWindowClickListener(this);
        mMap.setOnInfoWindowCloseListener(this);
        mMap.setOnMarkerDragListener(this);

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                markerPoints.add(latLng);
                MarkerOptions options = new MarkerOptions();
                // Setting the position of the marker
                options.position(latLng);
            }
        });

        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {    
                //Getting the coordinates
                toLatitude = marker.getPosition().latitude;
                toLongitude = marker.getPosition().longitude;

                dest = new LatLng(toLatitude, toLongitude);
                mMap.animateCamera(CameraUpdateFactory.newLatLng(dest));

                return ((toLatitude == my_marker.getPosition().latitude) && (toLongitude == my_marker.getPosition().longitude));
            }
        });
    }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        setUpMap();
    }

    private void setUpMap() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            Toast.makeText(MapsActivity.this, "Anda harus menyetujuinya agar dapat menikmati semua fitur yang ada", Toast.LENGTH_LONG).show();
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            }
        }

        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setTrafficEnabled(true);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .zoom(15)                   // Sets the zoom
                .target(new LatLng(-6.597629,106.79957))
                .tilt(40)                   // Sets the tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder
        mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
}

Here's the xml layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

This is the manifest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />

What's wrong with my code? Any help will be greatly appreciated.

Thanks.

2条回答
该账号已被封号
2楼-- · 2019-09-02 23:49

Try this,

SupportMapFragment mapFragment =  ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));

    if(mapFragment != null) {
       mapFragment.getMapAsync(this)
    }

and move code

mMap.setOnInfoWindowClickListener(this);
    mMap.setOnInfoWindowCloseListener(this);
    mMap.setOnMarkerDragListener(this);

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            markerPoints.add(latLng);
            MarkerOptions options = new MarkerOptions();
            // Setting the position of the marker
            options.position(latLng);
        }
    });

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {    
            //Getting the coordinates
            toLatitude = marker.getPosition().latitude;
            toLongitude = marker.getPosition().longitude;

            dest = new LatLng(toLatitude, toLongitude);
            mMap.animateCamera(CameraUpdateFactory.newLatLng(dest));

            return ((toLatitude == my_marker.getPosition().latitude) && (toLongitude == my_marker.getPosition().longitude));
        }
    });
}

to setUpMap() method, i tried here. its working. check once please

查看更多
Root(大扎)
3楼-- · 2019-09-02 23:51

First thing to notice is this method expects OnMapReadyCallback, As per the documentation. The major rules to be noted are,

  • This method must be called from the main thread.
  • In the case where Google Play services is not installed on the user's device, the callback will not be triggered until the user installs it.
  • In the rare case where the GoogleMap is destroyed immediately after creation, the callback is not triggered.

If these rules are not followed properly either you won't get proper result, or app may crash up. So I suggest you to use OnMapReadyCallback for getMapAsync method.

查看更多
登录 后发表回答