我正在尝试将GoogleMap对象实现到我的代码中。当我使用getMapASync()并将onMapReady()调用中创建的地图分配给我想要使用的GoogleMap对象时,它在该方法中工作,但是一旦我在该方法之外引用它,它就会说它仍然是null 。为什么? 公共类MainActivity扩展AppCompatActivity实现OnMapReadyCallback {
GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Map not connected", Toast.LENGTH_SHORT).show();
}
}
else{
setContentView(R.layout.activity_main);
}
String mapType2 = Integer.toString(mMap.getMapType());
Toast.makeText(this, mapType2 + " mMap", Toast.LENGTH_SHORT).show();
//This is for debugging purposes
}
public boolean servicesOK(){
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(isAvailable == ConnectionResult.SUCCESS){
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,this,ERROR_DIALOG_REQUEST);
dialog.show();
}
else{
Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap(){
if(mMap == null){
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
return (mMap != null);
}
@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
}
}
我得到的错误信息是:
FATAL EXCEPTION: main
Process: com.ramaya947yahoo.mymaps, PID: 1037
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ramaya947yahoo.mymaps/com.ramaya947yahoo.mymaps.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference
at com.ramaya947yahoo.mymaps.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
答案 0 :(得分:0)
因为你在跟踪
String mapType2 = Integer.toString(mMap.getMapType());
当map尚未初始化也未分配时,在onCreate
方法中。 所有与地图初始化相关的代码都应放在
中@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
//here
}
因为在已经创建活动之后的某个时间调用它。