0

我有以下代码:

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)
4

1 回答 1

0

您需要设置一个 MapReadyCallback。

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

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

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

于 2019-10-04T10:42:30.983 回答