2016-02-12 75 views
0

我嘗試建立一個谷歌地圖自定義標記的信息窗口,我點擊它,並且錯誤顯示點擊谷歌地圖自定義標記的信息窗口錯誤

TextView tvLat = (TextView)findViewById(R.id.tvLat);
空對象上 參考 我使用debug模式,

這是代碼檢查行,它有價值,但爲什麼作爲空對象?

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 = {[email protected]} 
marker = {[email protected]} 
v = {[email protected]} "android.widget.LinearLayout{1d737415 V.E..... ......I. 0,0-0,0}" 
latLng = {[email protected]} "lat/lng: (25.015243,121.472968)" 
tvLat = null 
tvLng = null 
latLng.longitude = 121.472968 
latLng.latitude = 25.015243 

回答

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

有關自定義信息窗口,see here更多信息。

+0

哦!有用!謝謝你很多!; D 對不起,問一個愚蠢的問題,它很爛是一個初學者;( –

相關問題