Capie Capie - 24 days ago 10
Android Question

MapFragment on a fragment

Im developing an app that displays a map on a fragment. Everything is set up, but there's small thing that is not letting me finish it.

gMapFragment.java:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class gMapFragment extends Fragment implements OnMapReadyCallback {


public gMapFragment() {
// Required empty public constructor
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

LatLng marker = new LatLng(46.33328, 15.38173);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker,13));
googleMap.addMarker(new MarkerOptions().title(("HERE WE ARE!")).position(marker));

}
}


I call this fragment from button in another frament and this is the method that calls it:

public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.SearchButton:
fragment = new gMapFragment();
replaceFragment(fragment);
break;
}
}

public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}


I get error "gMapFragment cannot be converted to fragment" Any idea how to fix this? thanks!

Answer

First, make sure that you're using import android.app.Fragment in all classes (ensure that you don't have import android.support.v4.app.Fragment; in any of your classes).

Then, add this to the Activity:

public void openMapFragment() {
    Fragment fragment = new gMapFragment();
    replaceFragment(fragment);
}

//moved to Activity from the Fragment
public void replaceFragment(Fragment someFragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, someFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

Then, in the Fragment onClick() method, just call into the Activity to do the FragmentTransaction:

public void onClick(View view) {
    switch (view.getId()) {
        case R.id.SearchButton:
            MainActivity activity = (MainActivity) getActivity();
            if (activity != null) {
                activity.openMapFragment();
            }
            break;
    }
}