0

我正在尝试使用MapView该类在我的 Android 应用程序中显示一个简单的地图。我在我的活动中使用以下onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        MapsInitializer.initialize(this);
    } catch (GooglePlayServicesNotAvailableException e) {
        Log.e("Address Map", "Could not initialize google play", e);
    }

    MapView mapView = new MapView(this);
    CameraUpdate camPos = CameraUpdateFactory.newLatLng(new LatLng(11.562276,104.920292));
    mapView.getMap().moveCamera(camPos);
    setContentView(mapView);
}

我有一个NullPointerException,因为方法mapView.getMap()返回null。不明白为什么,Google play 服务显然存在并已初始化。

4

4 回答 4

5

无法MapView上班,我终于结束了上课SupportMapFragment

对于那些可能有帮助的人,这是我的活动的完整代码:

public class AddressMap extends android.support.v4.app.FragmentActivity {

    private final static int FRAGMENT_ID = 0x101;
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        SupportMapFragment fragment = SupportMapFragment.newInstance();
        layout.setId(0x101);
        {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(FRAGMENT_ID, fragment);
            fragmentTransaction.commit();
        }

        setContentView(layout);

    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }


    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(FRAGMENT_ID)).getMap();
            if (mMap != null) {
                mMap.moveCamera(
                    CameraUpdateFactory.newLatLngZoom(new LatLng(11.562276, 104.920292), 10));
            }
        }
    }
}
于 2013-02-19T11:18:02.453 回答
2

此外,必须调用 mapView.onCreate()。

于 2014-12-10T10:40:53.693 回答
1

您正在尝试访问现已弃用的Google Maps Android v1 Api,请使用Google Maps Android Api v2

于 2013-02-18T12:18:08.803 回答
0

我像这样使用它并且它有效,这是新的api:

compile 'com.google.android.gms:play-services-maps:10.2.6'

public class MapViewFragment extends Fragment{
public static final String TAG = MapViewFragment.class.getSimpleName();

MapView mapView;
GoogleMap map;

static MapViewFragment instance;

public static MapViewFragment newInstance(){
    if(instance == null){
        instance = new MapViewFragment();
        return instance;
    }else{
        return instance;
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.i(TAG, "onCreateView: " );

    View view = inflater.inflate(R.layout.map_view_fragment, container, false);
    mapView = (MapView) view.findViewById(R.id.mvMap);
    if(mapView != null){

        mapView.onCreate(null);  //Don't forget to call onCreate after get the mapView!


        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                map = googleMap;
                //Do what you want with the map!!
            }
        });

        MapsInitializer.initialize(this.getActivity());
    }else{
        Log.i(TAG, "onCreateView: mapView is null");
    }

    return view;
}

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
  }
}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.gms.maps.MapView
    android:id="@+id/mvMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</com.google.android.gms.maps.MapView>

</LinearLayout>
于 2017-06-29T19:22:46.063 回答