在android上添加Google地图对象

在android上添加Google地图对象,android,android-xml,google-maps-android-api-2,Android,Android Xml,Google Maps Android Api 2,因此,我想在用户点击“添加圆”按钮时将对象添加到我拥有的映射中,例如一个圆,但当我尝试从xml中获取映射时,当它在newCircle()函数下执行.getmap()时,我会得到一个NullPointerException,有任何修复或解决方法吗?示例代码很有帮助 import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import a

因此,我想在用户点击“添加圆”按钮时将对象添加到我拥有的映射中,例如一个圆,但当我尝试从xml中获取映射时,当它在
newCircle()
函数下执行
.getmap()
时,我会得到一个
NullPointerException
,有任何修复或解决方法吗?示例代码很有帮助

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;

public class GoogleMaps extends FragmentActivity {
    GoogleMap mMap;

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

    private void makeCircleButton() {
        OnClickListener onClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                newCircle();
            }
        };

        Button button = (Button) findViewById(R.id.add_circular_area);
        button.setOnClickListener(onClickListener);
    }

    public void newCircle() {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

        // Instantiates a new CircleOptions object and defines the center and
        // radius
        CircleOptions circleOptions = new CircleOptions().center(
                new LatLng(37.4, -122.1)).radius(1000); // In meters
        mMap.addCircle(circleOptions);

        // Get back the mutable Circle
        // Circle circle = mMap.addCircle(circleOptions);
    }
xml:


xml包括:

<?xml version="1.0" encoding="utf-8"?>
<fragment
        xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:uiTiltGestures="false"
        map:mapType="hybrid"
        />

您使用的片段名称为
com.google.android.gms.maps.SupportMapFragment
,因此无法获取
MapFragment
您应该使用
getSupportFragmentManager()
而不是
getFragmentManager()
并将其强制转换为
SupportMapFragment

改变

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

希望这会有帮助

  mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();