【问题标题】:Google map custom marker info window setting error谷歌地图自定义标记信息窗口设置错误
【发布时间】:2016-05-22 17:22:55
【问题描述】:

我尝试设置一个自定义谷歌地图标记信息窗口,但日志总是说`

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) 

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    你得到了例外,因为你的 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;
            } 
        }); 
    
    } 
    

    【讨论】:

    • 哦!万分感谢!有用!只要我达到声望15,我就会投票给你!再次感谢您!
    猜你喜欢
    • 2023-03-21
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    相关资源
    最近更新 更多