如何正确保护GoogleMap免于空?
目前在drawOnMap()
方法中说明了mMap null,但在将其添加到onMapReady()
方法时有效。
我想知道,如果我在drawOnMap()
内onMapReady()
请求public class DetailActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String locationA;
private String locationB;
private ArrayList<LatLng> latLngList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// Check if Google Maps have been activated, else starting it again.
if (mMap == null) {
SupportMapFragment smf = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.map);
smf.getMapAsync(this);
}
// Settings up the views
TextView mDistance = (TextView) findViewById(R.id.distanceDetail);
TextView mDuration = (TextView) findViewById(R.id.durationDetail);
TextView mLocationA = (TextView) findViewById(R.id.locationA);
TextView mLocationB = (TextView) findViewById(R.id.locationB);
// Getting the values
Intent i = getIntent();
locationA = i.getStringExtra("locA");
locationB = i.getStringExtra("locB");
String distance = i.getStringExtra("distance");
String duration = i.getStringExtra("duration");
// initiate the values.
mDistance.setText(distance);
mDuration.setText(duration);
mLocationA.setText(locationA);
mLocationB.setText(locationB);
drawOnMap();
}
public void drawOnMap(){
latLngList = getIntent().getParcelableArrayListExtra("points");
// Används för att rita ut marksen på kartan sedan, start och stop.
// Get the first and last postion to write out the marks on map
LatLng startLatLng = latLngList.get(0);
LatLng endLatLng = latLngList.get(latLngList.size() - 1);
// Zooms into the start position;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startLatLng, 10));
// Adding mark to start location
mMap.addMarker(new MarkerOptions()
.position(startLatLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.title(locationA));
// Adding mark to end location
mMap.addMarker(new MarkerOptions()
.position(endLatLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.title(locationB));
if (latLngList.size() > 0) {
// Draw the route on map
mMap.addPolyline(new PolylineOptions()
.addAll(latLngList)
.color(Color.argb(255, 55, 160, 255))
.width(20));
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
UiSettings mapSettings = mMap.getUiSettings();
mapSettings.setZoomControlsEnabled(true);
}
之前,我可能会在从我的意图中获取值之前启动地图吗?
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startLatLng, 10));
{{1}}
目录下载
java.lang.NullPointerException:尝试调用虚方法'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)” 在null对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'无效 com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)” 在null对象引用上 在 com.example.rasmusjosefsson.rjcar.DetailActivity.drawOnMap(DetailActivity.java:113) 在 com.example.rasmusjosefsson.rjcar.DetailActivity.onCreate(DetailActivity.java:96) 在android.app.Activity.performCreate(Activity.java:6237) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
在地图准备好后调用drawOnMap方法。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
UiSettings mapSettings = mMap.getUiSettings();
mapSettings.setZoomControlsEnabled(true);
drawOnMap();
}
答案 1 :(得分:0)
您将使用onMapReady()
方法获取Google地图对象,以便从此处调用drawOnMap()
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Call drawOnMap() from here
}