我試圖在我的代碼中實現一個GoogleMap對象。當我使用getMapASync()並將在onMapReady()調用中創建的映射分配給我想要使用的GoogleMap對象時,它在該方法內起作用,但是一旦我在該方法之外引用它時,它說它仍然爲空。爲什麼? 公共類MainActivity擴展AppCompatActivity實現OnMapReadyCallback {在方法結束後,onMapReady()中的賦值不工作
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)
我明白什麼是NullPointerException,我在問爲什麼在賦值後仍然存在NullPointerException。鏈接的帖子不會幫助其他人有同樣的問題,但感謝您的意見。 –