Android:替换FrameLayout时出现NullPointerException

时间:2013-10-29 21:24:58

标签: android

我有以下代码:

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){

    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_left, null);

    // test

    SupportMapFragment fragment = new SupportMapFragment();
    FragmentManager fm = getActivity().getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.map, fragment).commit();

    GoogleMap googleMap = fragment.getMap();

    if(getCameraPosition() == null) { // first time in app
        setCameraPosition(new CameraPosition(getYourLatLng(), 12, 0 ,0));
    }
    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(getCameraPosition()));       

    googleMap.addMarker(new MarkerOptions()
    .title("Your Position")
    .snippet("This is where your smartphone is located.")
    .position(getYourLatLng()));

    return view;
}

这就是我要在xml文件中替换的内容:

     <FrameLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

这是怎么回事?无论如何更新id?

新筹码追踪:

10-29 22:37:52.277: E/AndroidRuntime(16273): FATAL EXCEPTION: main
10-29 22:37:52.277: E/AndroidRuntime(16273): java.lang.NullPointerException: CameraUpdateFactory is not initialized
10-29 22:37:52.277: E/AndroidRuntime(16273):    at com.google.android.gms.internal.s.b(Unknown Source)
10-29 22:37:52.277: E/AndroidRuntime(16273):    at com.google.android.gms.maps.CameraUpdateFactory.aX(Unknown Source)

1 个答案:

答案 0 :(得分:0)

您需要设置一个MapReadyCallback。

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

准备好地图后,您可以移动相机。 (有一个提供GoogleMap的功能,可让您移动相机。)

https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback.html