Android GoogleMaps:onMapReady没有被调用?

时间:2018-03-26 05:47:20

标签: android google-maps nullpointerexception

我在addMarker行上收到NullPointerException因为mGoogleMap对象未在onMapReady()中初始化,尽管我已调用getMapAsync()

Java文件:

    public class RideStart extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener,PlaceSelectionListener,OnMapReadyCallback,LocationListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener
    {


        String phone,username,email,ridetrackingno,GPSstart,GPSend;
        private GoogleMap nMap;
        MapView nmapview;
        GoogleMap mGoogleMap;
        SupportMapFragment mapFrag;
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        Location mLastLocation;
        Marker mCurrLocationMarker;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ride_start);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
            setSupportActionBar(toolbar);

            Log.d("Here!!","before initialization" );
            mapFrag= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mapFrag.getMapAsync((OnMapReadyCallback) this);

            Log.d("Here!!","after initialization" );


........//addMarker code --->NULL-POINTER exception bcoz mGooglemap isnt initialized
}
//onCreate ends.

public void onMapReady(GoogleMap googleMap) {
        Log.d("Here!!","in initialization" );
        LatLng sydney = new LatLng(12.843515, 77.663306);
        googleMap.addMarker(new MarkerOptions().position(sydney)
                .title("You are here"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        mGoogleMap=googleMap;
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }


}
//class ends
  

调试语句:清楚地显示“在初始化之前和之后”   初始化“但”在初始化中“永远不会打印。我不能   明白为什么会这样。

03-26 11:14:41.260 20046-20046/com.example.kulka.cabchain_partner D/Here!!: before initialization
03-26 11:14:41.262 20046-20046/com.example.kulka.cabchain_partner D/Here!!: after initialization
03-26 11:14:41.266 20046-20046/com.example.kulka.cabchain_partner D/AndroidRuntime: Shutting down VM
03-26 11:14:41.267 20046-20046/com.example.kulka.cabchain_partner E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.kulka.cabchain_partner, PID: 20046
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kulka.cabchain_partner/com.example.kulka.cabchain_partner.RideStart}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

1 个答案:

答案 0 :(得分:1)

在onCreate上添加标记将始终引用null ..因为地图尚未就绪

   @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_ride_start);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
                setSupportActionBar(toolbar);


            Log.d("Here!!","before initialization" );
            mapFrag= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mapFrag.getMapAsync((OnMapReadyCallback) this);

            Log.d("Here!!","after initialization" );



}
//onCreate ends.

public void onMapReady(GoogleMap googleMap) {
        Log.d("Here!!","in initialization" );
        LatLng sydney = new LatLng(12.843515, 77.663306);
        googleMap.addMarker(new MarkerOptions().position(sydney)
                .title("You are here"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        mGoogleMap=googleMap;
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //make a function for adding marker
    }


}
//class ends

从onCreate方法中删除标记代码并将其放在onMapReady方法下面,或者只是创建另一个方法并在onMapReady方法的末尾调用它。 这应该可以解决问题