My application has 4 tabs for that used ViewPager and in one of them I want to display a HERE MAPS . I was able to get the map to work perfectly in an Activity, but in Fragment Class when I try to cast the Fragment view to the MapFragment an error is thrown.
Here is the my XML code sample
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/transparent">
<!-- Map Fragment embedded with the map object -->
<fragment
android:id="@+id/mapfragment"
class="com.here.android.mpa.mapping.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Below my Fragment Code sample
public class CurrentLocationFragment extends Fragment {
// map embedded in the map fragment
private MapFragment mapFragment = null;
private Map map = null;
private static MapRoute mapRoute1;
private String locationAddress = "";
private Double latitude = 18.496252;
private Double langitude = 73.802118;
private static View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_current_location, container, false);
} catch (InflateException e) {
e.printStackTrace();
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initialize();
//displayMap();
}
private void initialize() {
// Search for the map fragment to finish setup by calling init().
mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.mapfragment);
mapFragment.init(new OnEngineInitListener() {
@Override
public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
if (error == OnEngineInitListener.Error.NONE) {
map = null;
map = mapFragment.getMap();
// Set the map center to the Vancouver region (no animation)
map.setCenter(new GeoCoordinate(latitude, langitude, 0.0),
Map.Animation.NONE);
map.getPositionIndicator().setVisible(true);
try {
Image img_current_location = new Image();
img_current_location.setImageResource(R.drawable.marker);
map.getPositionIndicator().setMarker(img_current_location);
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e("HEREMAP", "Cannot initialize MapFragment (" + error + ")");
}
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
try{
if (mapFragment != null)
getActivity().getFragmentManager().beginTransaction().remove(mapFragment).commit();
}catch (Exception e){
e.printStackTrace();
}
}
}
When we revisit the Map getting as InflateException of creating Map as below.
W/System.err: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f0a010b, tag null, or parent id 0xffffffff with another fragment for com.here.android.mpa.mapping.MapFragment
How to use HERE Map in Fragment.
Thank you in advance!!
The answer Matt suggests works, but it cause the map to be recreated and redrawn, which isn't always desirable. After lots of trial and error, I found a solution that works for me:
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
For good measure, here's "map.xml" (R.layout.map) with R.id.mapFragment (android:id="@+id/mapFragment"):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
User contributions licensed under CC BY-SA 3.0