【问题标题】:Click Google Map custom marker info window error点击谷歌地图自定义标记信息窗口错误
【发布时间】:2023-03-21 23:35:01
【问题描述】:

我尝试设置一个谷歌地图自定义标记信息窗口,我点击它,错误显示

TextView tvLat = (TextView)findViewById(R.id.tvLat);
在一个空对象上 参考 我使用调试模式,

这是代码检查那行,它有值,但为什么是一个空对象?

tvLat = null tvLng = 空 latLng.longitude = 121.472968 latLng.latitude = 25.015243

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);



}
@Override
public void onMapReady(final 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;
        }
    });

    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            Marker marker = map.addMarker(markerOptions);
            marker.showInfoWindow();
        }
    });

    map.addMarker(new MarkerOptions()
            .position(new LatLng(25.015243, 121.472968))
            .title("Hello world"));

        }

}

错误

 02-12 03:33:48.171 29581-29581/com.addtw.aweino1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.addtw.aweino1, PID: 29581
                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                   at com.addtw.aweino1.GoogleMapPage$1.getInfoContents(GoogleMapPage.java:54)
                                                                   at com.google.android.gms.maps.GoogleMap$3.zzc(Unknown Source)
                                                                   at com.google.android.gms.maps.internal.zzd$zza.onTransact(Unknown Source)
                                                                   at android.os.Binder.transact(Binder.java:395)
                                                                   at com.google.android.gms.maps.internal.n.b(SourceFile:112)
                                                                   at com.google.maps.api.android.lib6.e.i.a(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.e.i.a(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.c.e.b(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.c.g.c(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.e.ag.g(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.e.ai.b(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.c.e.a(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.m.av.a(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.m.be.a(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.m.bd.a(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.m.bt.d(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.gmm6.m.ak.onSingleTapConfirmed(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.d.g.onSingleTapConfirmed(Unknown Source)
                                                                   at com.google.maps.api.android.lib6.d.i.handleMessage(Unknown Source)
                                                                   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)

调试

this = {GoogleMapPage$1@20061} 
marker = {Marker@20064} 
v = {LinearLayout@20065} "android.widget.LinearLayout{1d737415 V.E..... ......I. 0,0-0,0}"
latLng = {LatLng@20066} "lat/lng: (25.015243,121.472968)"
tvLat = null
tvLng = null
latLng.longitude = 121.472968
latLng.latitude = 25.015243

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    你需要在你膨胀的视图上调用findViewById()。

    所以,应该改为v.findViewById():

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

    有关自定义 InfoWindows 的更多信息,see here。

    【讨论】:

    • 哦!有用!非常感谢!;不好意思问愚蠢的问题,作为初学者很糟糕;(
    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    相关资源
    最近更新 更多