简体   繁体   English

创建地图v2扩展片段时出错

[英]Error on creating map v2 extend fragment

I tried to implement google map v2 on fragment..Here is the code : 我试图在片段上实现google map v2。这是代码:

package com.example.tropo;
public class D_Map extends Fragment {
    public static final String TAG = "map";
    MapView mapView;
    GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.d_map, container, false);

    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) v.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
    map.getUiSettings().setMyLocationButtonEnabled(false);
    map.setMyLocationEnabled(true);

    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    try {
        MapsInitializer.initialize(this.getActivity());
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Updates the location and zoom of the MapView
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
    map.animateCamera(cameraUpdate);

    return v;
}

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

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

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

}

logcat: 日志猫:

07-08 04:08:08.590  21776-21776/com.myapps.materialapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.tropo.D_Map.onCreateView(D_Map.java:49)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
            at android.app.BackStackRecord.run(BackStackRecord.java:635)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

In Google Play services 6.5 and per the MapFragment documentation : Google Play服务6.5中和根据MapFragment文档

A GoogleMap must be acquired using getMapAsync(OnMapReadyCallback) . 必须使用getMapAsync(OnMapReadyCallback)来获取GoogleMap。 This class automatically initializes the maps system and the view. 此类会自动初始化地图系统和视图。

Use that callback rather than getMap() to ensure that the map is not null . 使用该回调而不是getMap()来确保地图不为null

getMap is deprecated.Use getMapAsync(OnMapReadyCallback) instead. 不建议使用getMap。请改用getMapAsync(OnMapReadyCallback)。 The callback method provides you with a GoogleMap instance guaranteed to be non-null and ready to be used. 回调方法为您提供了一个保证为非null且可以使用的GoogleMap实例。

Also, 也,

getmap returns Null if the view of the map is not yet ready. 如果地图的视图尚未准备好,则getmap返回Null。 This can happen when Google Play services is not available. 当Google Play服务不可用时,可能会发生这种情况。 If Google Play services becomes available afterwards, calling this method again will initialize and return the GoogleMap. 如果此后可以使用Google Play服务,则再次调用此方法将初始化并返回GoogleMap。

https://developers.google.com/android/reference/com/google/android/gms/maps/MapView https://developers.google.com/android/reference/com/google/android/gms/maps/MapView

Try using the SupportMapFragment.Add it to your activity layout 尝试使用SupportMapFragment将其添加到活动布局

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity"
        tools:layout="@layout/abc_activity_chooser_view" />

get map in the activity using 在活动中使用获取地图

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM