java - 创建 map v2 扩展 fragment 时出错

标签 java android android-fragments android-mapview android-maps-v2

我尝试在 fragment 上实现谷歌地图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();
}

}

日志猫:

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)

最佳答案

Google Play services 6.5并根据MapFragment documentation :

A GoogleMap must be acquired using getMapAsync(OnMapReadyCallback). This class automatically initializes the maps system and the view.

使用该回调而不是 getMap() 来确保 map 不 null

关于java - 创建 map v2 扩展 fragment 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283372/

相关文章:

android - 不同 Activity fragment 之间的共享元素转换

java - 与数字关联的最小基数

java - 使用 GSON 反序列化两种不同的日期格式

android - 允许或拒绝将我的应用程序复制到 SD 卡?

android - 如何构建签名包 (AAB)?

Android:何时设置Fragment的工具栏标题?

java - 线程中的异常 "AWT-EventQueue-0"java.lang.IllegalArgumentException : input == null

java - 父类中的子实例

java - 在 Android 模拟器上运行 facebook 应用程序

android - 如何退出 fragment 并从工具栏项返回到上一个 Activity 或 fragment