Google map not displayed in Fragment subclass - android

I am created a map and it displayed in fragment in another activity .In my application has only one Activity class.I need to display it in Fragment .
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapAtm"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
public class Atm extends Fragment {
private SupportMapFragment mSupportMapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.atm_main,
container, false);
mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapAtm);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.mapAtm, mSupportMapFragment).commit();
}
if (mSupportMapFragment != null) {
mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap != null) {
googleMap.getUiSettings().setAllGesturesEnabled(true);
LatLng marker_latlng = new LatLng(150, -150);
CameraPosition cameraPosition = new CameraPosition.Builder().target(marker_latlng).zoom(15.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.moveCamera(cameraUpdate);
}
}
});
}
return view;
}
}
Their is no error in Emulator.But after installing in Mobile;application will terminate when clicking in Atm Map

Related

Android: adding polygon to Map from ArrayList in a Fragment

I have a Fragment that contains a map, this map is loaded up on the callback and OnMapReady which pulls in a map at my location successfully.
However I am trying to add in a feature whereby the user selects a location from a Spinner and the selection then draws the relevant Polygon on my map, however I keep getting an error 'Attempting to invoke virtual method Polygon'
I am by no means a talented Android developer, trying to learn and play about somewhat.
public class FindCarpark extends Fragment {
private SupportMapFragment mapFragment;
private UserLocation.OnFragmentInteractionListener mListener;
Spinner spinCar;
String selectedItem;
GoogleMap mMap;
private static final ArrayList<LatLng> cpB = new ArrayList<>();
public FindCarpark() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_find_carpark, container, false);
Context context = getActivity().getApplicationContext();
spinCar = rootView.findViewById(R.id.car_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),R.array.car_parks, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinCar.setAdapter(adapter);
spinCar.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (i > 0) {
selectedItem = spinCar.getSelectedItem().toString().trim();
addCoords(selectedItem.toString());
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
new googleMap().makeMap(mMap);
}
});
}
getChildFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
return rootView;
}
public void addCoords(String coords){
Log.i("coords", coords);
switch (coords){
case "B":
cpB.add(new LatLng(53.797731, -1.546351));
cpB.add(new LatLng(53.797551, -1.545983));
cpB.add(new LatLng(53.797447, -1.545267));
cpB.add(new LatLng(53.797731, -1.546351));
makePolygon(cpB);
Log.i("coords", cpB.toString());
break;
default:
break;
}
}
public void makePolygon(ArrayList<LatLng> coords){
mMap.addPolygon(new PolygonOptions()
.addAll(coords)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
}
XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FindCarpark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="#+id/car_spinner"
android:prompt="#string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
</Spinner>
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Polygon com.google.android.gms.maps.GoogleMap.addPolygon(com.google.android.gms.maps.model.PolygonOptions)' on a null object reference
Your mMap variable is never filled. This then later causes a null pointer exception. The problem is:
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
new googleMap().makeMap(mMap);
}
});
}
here mMap is a local variable, while the mMap you later try to access is a property of the object, thus:
if (mapFragment == null) {
mapFragment = SupportMapFragment.newInstance();
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap mMap) {
FindCarpark.this.mMap = mMap
new googleMap().makeMap(mMap);
}
});
}

Add Marker to Google Map in SupportFragment

I did a tabbed-activity with for tabs. In the fourth tab I did a map, and I added a marker but the marker is not visible and I do not undertand why. I tried many solutions for my problem, but for now I have come to this situation.
This is my Java code:
public class tab_mappa extends SupportMapFragment implements OnMapReadyCallback {
private GoogleMap googleMap;
SupportMapFragment mSupportMapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.tabmappa, null, false);
mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.map, mSupportMapFragment).commit();
}
return root;
}
#Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
this.googleMap.getUiSettings().setZoomControlsEnabled(true);
this.googleMap.getUiSettings().setCompassEnabled(true);
this.googleMap.getUiSettings().setMyLocationButtonEnabled(true);
this.googleMap.getUiSettings().setZoomGesturesEnabled(true);
this.googleMap.getUiSettings().setRotateGesturesEnabled(true);
LatLng pos1 = new LatLng(44.783878,10.879663);
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pos1);
this.googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
}
});
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
getMapAsync(this);
}
}
}
This is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="vertical"
tools:context="esame.progetto.xhondar.github.com.info.tab_mappa">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.03"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map" />
</LinearLayout>
And this is my final result:
Do you have any ideas?
I solved the problem, this is the solution for those who need it:
Java code:
import android.support.v4.app.Fragment;
public class tab_mappa extends Fragment implements OnMapReadyCallback {
GoogleMap map;
SupportMapFragment mapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tabmappa, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return v;
}
#Override
public void onMapReady(GoogleMap googleMap){
map = googleMap;
this.map = googleMap;
this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
this.map.getUiSettings().setZoomControlsEnabled(true);
this.map.getUiSettings().setCompassEnabled(true);
this.map.getUiSettings().setMyLocationButtonEnabled(true);
this.map.getUiSettings().setZoomGesturesEnabled(true);
this.map.getUiSettings().setRotateGesturesEnabled(true);
LatLng pp = new LatLng(44.783878,10.879663);
map.addMarker(new MarkerOptions().position(pp).title("Carpi"));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(pp, 8));
}
}
Xml code:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="esame.progetto.xhondar.github.com.info.tab"/>
The error was located in xml code: is Fragment and not FrameLayout, this thing brought conflict within the code.
How about use marker without the builder? you wrote the code in onMapReady()
LatLng pos1 = new LatLng(44.783878,10.879663);
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pos1);
this.googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
}
});
it change to
LatLng pos1 = new LatLng(44.783878,10.879663);
googleMap.addMarker(new MarkerOptions().position(pos1));
this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
}
});
this one. I removed builder and this before googleMap.

onMapReady Marker does not work

i Have a Fragment that should show a Specific place on Google Map
public class ContactFragment extends Fragment implements OnMapReadyCallback {
public ContactFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentManager fragment = getActivity().getSupportFragmentManager();
final SupportMapFragment myMAPF = (SupportMapFragment) fragment.findFragmentById(R.id.map);
return inflater.inflate(R.layout.fragment_contact, container, false);
}
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(34.877737,-1.326545);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker,13));
googleMap.addMarker(new MarkerOptions().position(marker));
}
}
and in the XML file of the Fragment i have this :
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:layout_below="#+id/contact_domiciliation"
class="com.google.android.gms.maps.MapFragment"
></fragment>
Yet when i launch click on the button that opens this fragment i get a map of the entire globe .
Add this line of code to the fragment:
myMAPF.getMapAsync(this);

Using fragment for Google map in xml showing error of "android.view.InflateException: Binary XML file line #57: Error inflating class fragment"

I am working on a project in which I need to show a Map in home screen. I used fragment class.
When I first lunch my screen, it is showing map successfully. But when I again click on Home or again come back from another screen to that map screen, below error is showing and app crashes.
"android.view.InflateException: Binary XML file line #57: Error inflating class fragment"
Here is my xml class.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/homemap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
map:cameraTilt="50"
map:cameraZoom="40" />
</RelativeLayout>
And below is my Java class
public class Customer_Home_Map extends Fragment {
public Customer_Home_Map() {
}
private GoogleMap theMap;
ProgressDialog progressDialog = null;
ArrayList<SafetyResults_SearchResult> lstresult;
double lati = 0.0, longi = 0.0;
GPSTracker gps;
GoogleMap mMap;
SharedPreferences sharedPreferences;
int locationCount = 0;
API api = API.getInstance();
private HashMap<Marker, MyMarker> mMarkersHashMap;
private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView = inflater.inflate(R.layout.customer_home_map,
// container,
// false);
View view = inflater.inflate(R.layout.customer_home_map,
null);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
up.getFont_Lato_Regular());
gps = new GPSTracker(getActivity());
showAddress();
return view;
}
private void showAddress() {
if (gps.canGetLocation()) {
lati = gps.getLatitude();
longi = gps.getLongitude();
straddress = getMyLocationAddress(lati, longi);
}
mMarkersHashMap = new HashMap<Marker, MyMarker>();
setUpMap();
plotMarkers(mMyMarkersArray);
}
private void plotMarkers(ArrayList<MyMarker> markers) {
if (markers.size() > 0) {
for (MyMarker myMarker : markers) {
// Create user marker with custom icon and other options
MarkerOptions markerOption = new MarkerOptions()
.position(new LatLng(myMarker.getmLatitude(), myMarker
.getmLongitude()));
markerOption.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
private void setUpMap() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.homemap)).getMap();
LatLng coordinate = new LatLng(lati, longi);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(
coordinate, 15);
mMap.animateCamera(yourLocation);
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(
com.google.android.gms.maps.model.Marker marker) {
marker.showInfoWindow();
return true;
}
});
} else
Toast.makeText(getActivity().getApplicationContext(),
"Unable to create Maps", Toast.LENGTH_SHORT).show();
}
}
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
public MarkerInfoWindowAdapter() {
}
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(
R.layout.infowindow_layout, null);
MyMarker myMarker = mMarkersHashMap.get(marker);
ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
TextView markerLabel = (TextView) v.findViewById(R.id.marker_label);
markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));
markerLabel.setText(myMarker.getmLabel());
TextView anotherLabel = (TextView) v
.findViewById(R.id.another_label);
// anotherLabel.setText("A custom text");
return v;
}
}
private int manageMarkerIcon(String markerIcon) {
if (markerIcon.equalsIgnoreCase("3"))
return R.drawable.ambulance;
else if (markerIcon.equalsIgnoreCase("4"))
return R.drawable.doctor;
else if (markerIcon.equalsIgnoreCase("me"))
return R.drawable.marker;
else
return R.drawable.marker;
}
}
I am attaching my logcat error snap below
this is xml code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".Googlemap.Googlemap">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
and code should be something like this
private GoogleMap googleMap;
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
}
hopefully this will work.
I searched a lot and now I found solutions of my problem.
I write below code in my activity.
#Override
public void onDestroyView() {
try {
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.homemap);
if (getActivity().isFinishing()) {
}
else if (f != null)
getActivity().getFragmentManager().beginTransaction().remove(f)
.commit();
} catch (Exception e) {
}
super.onDestroyView();
}
Thanks All for giving you valuable time and suggestions.

I want to show another google maps fragment v2

I'm developing android tablet project. There is dualpane layout. In the left side there is leftscrollfrg fragment, it's also execute right side, mainmapfragment, it opens the map. In the left side, i click createproject button and when i click the button it hides the mainmapfragment and adds another fragment which is viewpager fragment. In the first page there is CreateProjectFrg1 fragment and inside there is button which adds another map fragment (CreateProjectMapFrg).
The problem is when the second map opened, the first one is showed like frozen. But i can see the new opened map because the old one padded from left and right. And also when i turn off the screen and back on, the old one map which frozen is gone.
How can i solve this problem?
Note: In the hierarchy view, i cannot see the frozen map.
public class LeftScrollFrg extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.frg_left_behind, container, false);
frg_map = (MainMapFragment) getActivity()
.getSupportFragmentManager().findFragmentByTag(
MAP_FRAGMENT_TAG);
if (frg_map == null) {
frg_map = new MainMapFragment();
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
ft.addToBackStack(MAP_FRAGMENT_TAG);
ft.commit();
}
imgbtn_createproject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hideMainMapFrg();
openCreateProjectFrg();
}
});
imgbtn_map.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hideCreateProjectFrg();
openMapFrg();
}
});
return vw;
}
private void hideMainMapFrg() {
fm = getActivity().getSupportFragmentManager();
frg_map = (MainMapFragment) fm
.findFragmentByTag(MAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_map != null) {
if (frg_map.isVisible()) {
ft.hide(frg_map);
}
}
}
ft.commit();
}
private void openMapFrg() {
fm = getActivity().getSupportFragmentManager();
frg_map = (MainMapFragment) fm
.findFragmentByTag(MAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_createproject != null) {
if (frg_createproject.isVisible()) {
ft.hide(frg_createproject);
}
}
if (frg_map == null) {
frg_map = new MainMapFragment();
ft.add(R.id.above_framelayout, frg_map, MAP_FRAGMENT_TAG);
} else {
if (frg_map.isHidden()) {
ft.show(frg_map);
}
}
ft.addToBackStack(MAP_FRAGMENT_TAG);
ft.commit();
}
}
}
public class MainMapFragment extends SupportMapFragment implements
LocationListener, LocationSource, OnInfoWindowClickListener,
OnMapClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.frg_mainproject, container, false);
return view;
}
}
//frg_mainproject.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/rightpanel_margin" >
<fragment
android:id="#+id/main_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</RelativeLayout>
public class CreateProjectFrg1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.frg_createproject1, container,
false);
imgvw_createprojectmap.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openCreateMap();
}
});
return vw;
}
private void openCreateMap() {
fm = getActivity().getSupportFragmentManager();
frg_map = (CreateProjectMapFrg) fm
.findFragmentByTag(CREATEPROJECTMAP_FRAGMENT_TAG);
if (fm != null) {
ft = fm.beginTransaction();
if (frg_map == null) {
frg_map = new CreateProjectMapFrg();
ft.add(R.id.above_framelayout, frg_map, CREATEPROJECTMAP_FRAGMENT_TAG);
}
ft.addToBackStack(CREATEPROJECTMAP_FRAGMENT_TAG);
ft.commit();
}
}
}
public class CreateProjectMapFrg extends SupportMapFragment implements
OnMapClickListener, OnMarkerClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.frg_createprojectmap, container,
false);
} catch (InflateException e) {
}
setViewsResource(view);
setUpMapIfNeeded();
return view;
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(
R.id.frg_createproject_map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
}
//frg_createprojectmap.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/frg_createproject_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Updated code
This is https://code.google.com/p/gmaps-api-issues/issues/detail?id=5027
As a workaround you can explicitely remove / replace the MapFragment from the fragment manager instead of just hiding it. If you don't want to remove the fragment as a whole, you can also implement your own Fragment that just uses MapView and detaches that just before the fragment is hidden (might need some more custom code).

Categories

Resources