Issue
I am working on a app that has 4 tabs, and 1 tab is google map.
When the user click on it , it can create the map correctly, however, if the user leave the current tab by click on other tab, and come back to this tab again, it will throw exception of error inflating layout XML.
So I log the flow, I found that in the onCreateView the second time , rootview is already create, but when I add if (rootView == null)
, it still throw error of "need to removeView on child view" . How to fix that? Thanks.
public class Map extends Fragment {
// Google Map
private GoogleMap googleMap;
private boolean isShowRoute;
private Database db;
private ArrayList<Restaurant> rList;
private ArrayList<Spot> sList;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("test1","" + rootView);
rootView = inflater.inflate(R.layout.map, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("test1","b");
super.onActivityCreated(savedInstanceState);
db = new Database (getActivity());
rList = db.getRestaurant();
sList = db.getSpot();
try {
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
} else {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
if (isShowRoute)
showRoute();
else
pinMap();
}
}
}
private void showRoute() {
// if (RunTracker.routePoints.size() > 1){
// Polyline route = googleMap.addPolyline(new PolylineOptions()
// .geodesic(true));
// route.setPoints(RunTracker.routePoints);
//
// LatLng lastLatLng = RunTracker.routePoints.get(RunTracker.routePoints.size() - 1);
// googleMap.addMarker(new MarkerOptions().position(lastLatLng).title("You are here"));
// googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(lastLatLng, 16.0f) );
// }
}
private void pinMap() {
for (Restaurant r : rList) {
MarkerOptions marker = new MarkerOptions().position(new LatLng(r.lat, r.lng)).title(r.name);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
googleMap.addMarker(marker);
}
for (Spot s : sList) {
MarkerOptions marker = new MarkerOptions().position(new LatLng(s.lat, s.lng)).title(s.name);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(marker);
}
googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(22.3079136,114.1778376), 10.0f) ); // go to MK MTR station by default
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
Log cat
03-31 14:53:56.669: E/AndroidRuntime(4611): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f050006, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Update Code:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
db = new Database (getActivity());
if (rList == null && sList == null) {
rList = db.getRestaurant();
sList = db.getSpot();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.map, container, false);
try {
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
Solution
@Override
public void onDestroyView() {
super.onDestroyView();
if (mMap != null) {
getActivity().getSupportFragmentManager().beginTransaction().remove(getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).commit();
mMap = null;
}
}
The mapfragment's id must be removed from the FragmentManager or else if the same it is passed on the next time then app will crash
Answered By - user782104
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.