Google地图自定义标记信息窗口设置错误

时间:2016-02-11 18:56:45

标签: android google-maps

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

  

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) 

1 个答案:

答案 0 :(得分:2)

您收到异常,因为此时def matrixTranspose(X): result=[[0 for k in range (len(X))] for l in range (len(X[0]))] for i in range(len(X)): for j in range (len(X[0])): result[i][j+1]=X[i][j] return result m1=[] r=input("enter The no. of rows:") c=input("enter The no. of columns:") s=[[0 for i in range (c)]for j in range (r)] for i in range (r): row=[] for j in range (c): x=input("Enter Value") row.append(x) m1.append(row) print m1 m2=matrixTranspose(m1) print m2 对象为map。如果您想在地图上调用某个函数,请在null函数中读取地图时调用它。

onMapReady