我尝试设置谷歌地图自定义标记信息窗口,我点击它,错误显示
TextView tvLat = (TextView)findViewById(R.id.tvLat);
在null对象上 参考 我使用调试模式,
这是代码检查行,它有值,但为什么作为空对象?
tvLat = null tvLng = null 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
答案 0 :(得分:3)
您需要在展开的视图上调用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。