0

次のコードがあります。

public View onCreateView(LayoutInflater インフレータ、ViewGroup コンテナ、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

マップの準備ができたら、カメラを移動できます。(カメラを移動できるGoogleMapを提供する機能があります。)

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

于 2019-10-04T10:42:30.983 に答える