2016-02-11 32 views
0

我尝试建立一个自定义的谷歌地图标记信息窗口,但日志总是说'谷歌地图自定义标记信息窗口设置错误

map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()

对空对象引用

我遵循示例,google很多,但无法找到答案,请帮忙;)

这是代码

public class GoogleMapPage extends FragmentActivity implements OnMapReadyCallback { 


private GoogleMap map; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.googlemap); 
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
     @Override 
     public View getInfoWindow(Marker marker) { 
      return null; 
     } 
     @Override 
     public View getInfoContents(Marker marker) { 
      View v = getLayoutInflater().inflate(R.layout.mapinfowindow,null); 
      LatLng latLng =marker.getPosition(); 
      TextView tvLat = (TextView)findViewById(R.id.tvLat); 
      TextView tvLng = (TextView)findViewById(R.id.tvLng); 
      tvLat.setText(String.valueOf(latLng.latitude)); 
      tvLng.setText(String.valueOf(latLng.longitude)); 
      return v; 
     } 
    }); 

} 
@Override 
public void onMapReady(GoogleMap map) { 

     } 

错误

02-12 02:45:23.131 638-638/com.addtw.aweino1 E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.addtw.aweino1, PID: 638 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.addtw.aweino1/com.addtw.aweino1.GoogleMapPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setInfoWindowAdapter(com.google.android.gms.maps.GoogleMap$InfoWindowAdapter)' on a null object reference 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5910) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setInfoWindowAdapter(com.google.android.gms.maps.GoogleMap$InfoWindowAdapter)' on a null object reference 
at com.addtw.aweino1.GoogleMapPage.onCreate(GoogleMapPage.java:37) 
at android.app.Activity.performCreate(Activity.java:6178) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)  
at android.app.ActivityThread.access$900(ActivityThread.java:177)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:135)  
at android.app.ActivityThread.main(ActivityThread.java:5910)  
at java.lang.reflect.Method.invoke(Native Method)  
at java.lang.reflect.Method.invoke(Method.java:372)  

回答

3

你得到的异常,因为你的map对象是null的那一刻。如果你想在地图上调用函数,那么在onMapReady函数中读取地图时调用它。

@Override 
    public void onMapReady(GoogleMap map) { 

    this.map = map; 

    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
     @Override 
     public View getInfoWindow(Marker marker) { 
      return null; 
     } 
     @Override 
     public View getInfoContents(Marker marker) { 
      View v = getLayoutInflater().inflate(R.layout.mapinfowindow,null); 
      LatLng latLng =marker.getPosition(); 
      TextView tvLat = (TextView)findViewById(R.id.tvLat); 
      TextView tvLng = (TextView)findViewById(R.id.tvLng); 
      tvLat.setText(String.valueOf(latLng.latitude)); 
      tvLng.setText(String.valueOf(latLng.longitude)); 
      return v; 
     } 
    }); 

} 
+1

哦!万分感谢!有用! 只要我达到声望15,我会投票给你! 再次感谢你! –

相关问题