Google地图自定义标记信息窗口设置错误 [英] Google map custom marker info window setting error
本文介绍了Google地图自定义标记信息窗口设置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图建立一个自定义的google地图标记信息窗口,但是日志总是说`
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()
关于空对象引用
我按照样例,google很多,但是找不到答案,请帮忙;)
这是代码
public class GoogleMapPage extends FragmentActivity implements OnMapReadyCallback {
$
私人GoogleMap地图;
@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;
}
});
$ b @Override
public void onMapReady(GoogleMap map){
$ b $}
$ p $02-12 02:45:23.131 638- 638 / com.addtw.aweino1 E / AndroidRuntime:FATAL EXCEPTION:main
进程:com.addtw.aweino1,PID:638
java.lang.RuntimeException:无法启动活动ComponentInfo {com.addtw。 aweino1 / com.addtw.aweino1.GoogleMapPage}:java.lang.NullPointerException:尝试调用虚方法void com.google.android.gms.maps.GoogleMap.setInfoWindowAdapter(com.google.android.gms.maps.GoogleMap $ (ActivityThread.java:2769)
在空对象引用
处android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
在android.app.ActivityThread.access $ 900(ActivityThread.java:177)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(处理器.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方法)$ b $在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1405)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
导致:java.lang.NullPointerException:尝试调用虚拟方法'void com.addtw.aweino1.GoogleMapPage.onCreate(GoogleMapPage)上的空对象引用
上的com.google.android.gms.maps.GoogleMap.setInfoWindowAdapter(com.google.android.gms.maps.GoogleMap $ InfoWindowAdapter) .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)$ b $ at java.lang.reflect.Method.invoke(Method.java:372)
解决方案
你得到异常是因为你的 map
对象当时是 null
。如果你想在map上调用一个函数,那么当你的地图在 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;
}
});
}
I try to set up a custom google map marker info window, but the log always said `
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()
on a null object reference
I follow the sample ,google a lot, but can't find the answer , Help please ;)
This is the code
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) {
}
Error
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)
解决方案
You get the exception because your map
object is null
at that moment. If you want to call a function on map then call it when your map is read in onMapReady
function.
@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;
}
});
}
这篇关于Google地图自定义标记信息窗口设置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文