2017-10-04 28 views
0

我試圖在我的代碼中實現一個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)  
+0

我明白什麼是NullPointerException,我在問爲什麼在賦值後仍然存在NullPointerException。鏈接的帖子不會幫助其他人有同樣的問題,但感謝您的意見。 –

回答

0

它,因爲你是在onCreate ivoking

String mapType2 = Integer.toString(mMap.getMapType()); 

方法,當地圖尚未初始化ializer也沒有分配。 所有地圖初始化相關的代碼應放置在

@Override 
public void onMapReady(GoogleMap map) { 
    this.mMap = map; 
//here 

} 

由於這被稱爲後活動已經創造了一些時間。

+0

我現在明白了,謝謝你的澄清! –

+0

我正在學習一個Lynda.com教程,並且我嘗試的原始方式是他們如何表現出來,但似乎過時了,因爲他們仍然使用getMap()而不是getMapASync()。 –

+0

它將與getMap協同工作,因爲它會阻止呼叫,直到地圖準備就緒。這是異步版本。保證使用一些低質量的「教程」爲什麼不檢查有關如何使用Android上的地圖谷歌官方文檔? – Antoniossss