The map shows up perfectly when I first select the tab fragment, however if I navigate to other tab and reselect the map fragment tab it will throws me this error - "Binary XML file line #9: Error inflating class fragment". I have look on other similar threads, some of them suggesting that my class should extend fragment activity instead and I don't want to do that.
This is my MapActivity.java:
public class MapActivity extends Fragment implements ActionBar.TabListener{
private Fragment mFragment;
private GoogleMap map;
private 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.activity_map, container, false);
MapsInitializer.initialize(view.getContext());
} catch (InflateException exp) {
exp.printStackTrace();
/* map is already there, just return view as it is */
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
return view;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mFragment = new MapActivity();
fragmentTransaction.replace(android.R.id.content, mFragment);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
fragmentTransaction.remove(mFragment);
if(httpAsyncTask!=null)
httpAsyncTask.setCanceled(true);
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
This is my xml :
<LinearLayout 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:orientation="vertical"
android:weightSum="100"
tools:context=".MapActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="90"
class="com.google.android.gms.maps.MapFragment" />
<Button
android:id="@+id/map_get_direction"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:background="@drawable/getdirection_button" />
</LinearLayout>