1

Использую Google Map, разместил компоненту во фрагмент.

Вот разметка фрагмента:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_weight="1" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchMy"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:src="@drawable/ic_track_changes_100dp"
        android:background="@drawable/button_circle_selector"
        android:padding="10dp"
        android:onClick="onClickSearchMyLocation" />
</FrameLayout>

Вот сам код который работает на SDK 22 и SDK 23 :

public class fragment_new_order_map extends Fragment {
    MapView mMapView;
    private GoogleMap googleMap;

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

        View v = inflater.inflate(R.layout.fragment_new_order_map, container, false);

        mMapView = (MapView) v.findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap = mMapView.getMap();

        if (googleMap != null) {}   
}

В итоге начиная с SDK 16 = googleMap возвращается NULL.

Смотрим пример который есть в Android Studio и меняем все как там:

public class fragment_new_order_map extends Fragment implements  OnMapReadyCallback {

...

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

        SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
}

@Override
    public void onMapReady(GoogleMap googleMap) {}

Но так выдает ошибку в принципе. Везде. Где то что то не правильно делаю. Ошибка :

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference at ru.suite.name.project.fragments.fragment_new_order_map.onCreateView

1 ответ 1

0

Разобрался!

Чтобы подключить карту в фрагменте:

googleMap = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {}

Но возникла другая проблема. Но это тему для другого топа.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.