Android googlemap中的NullPointerException

时间:2014-03-03 23:05:09

标签: android google-maps-api-3 nullpointerexception

我在初始化GoogleMap时收到NullPointerException,

以下是我的代码:

 public class MapActivity extends FragmentActivity {

    private GoogleMap mapa = null;
    private double lat, lng;
    private double oldlat, oldlng;
    private String titulo;
    private boolean marcador;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        lat = 0;
        lng = 0;
        oldlat = 0;
        oldlng = 0;
        marcador = false;

        mapa.setOnMapClickListener(new OnMapClickListener() {
            public void onMapClick(LatLng point) {
                Projection proj = mapa.getProjection();
                Point coord = proj.toScreenLocation(point);

                Toast.makeText(
                        MapActivity.this, 
                        "Estás en :\n" + 
                        "Lat: " + point.latitude + "\n" +
                        "Lng: " + point.longitude + "\n" +
                        "X: " + coord.x + " - Y: " + coord.y,
                        Toast.LENGTH_SHORT).show();
            }
        });

        mapa.setOnCameraChangeListener(new OnCameraChangeListener() {
            public void onCameraChange(CameraPosition position) {
                    lat = position.target.latitude;
                    lng = position.target.longitude;
            }
        });

        mapa.setOnMarkerClickListener(new OnMarkerClickListener() {
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(
                        MapActivity.this, marker.getTitle(),
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });

    }

Api密钥写得很好,异常就在这行......

mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

AndroidManifest很好,就像布局一样:

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-feature android:glEsVersion="0x00020000"
          android:required="true"/>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
   [...]
     <meta-data android:name="com.google.android.maps.v2.API_KEY"
           android:value="AIzaSyAwfktd5wjjUPaf4cCxQzP3IKZxiTNTiwc"/>
        <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
        <uses-library android:name="com.google.android.maps" />
</application>
</manifest>

2 个答案:

答案 0 :(得分:2)

我在xml中的视图是这样的,它不是MapFragment,而是SupportMapFragment

<fragment
          android:id="@+id/map"
          android:layout_width="400dp"
          android:layout_height="400dp"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

答案 1 :(得分:0)

更改

mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

mapa = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

您使用地图片段不支持地图片段。