0

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>
June Leow
  • 102
  • 1
  • 16

2 Answers2

0

You canont remove/replace fragments that you used directly in your XML files (treat your layout structure from XML as read-only). If you want to be able to add/remove map or other fragments, you must remove it from XML and add from code (i.e. on onCreateView())

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • You mean I should remove the map fragment from xml and add it from code? – June Leow Jul 16 '14 at 09:08
  • yes. remove map fragment form XML, put empty container in that place (i.e. `FrameLayout`) then add your fragment to that container from code so you'd be able to remove/replace it later – Marcin Orlowski Jul 16 '14 at 09:35
-1

The error probably says that it can't inflate because an entity already exists? If you want to do it this way you need to remove the reference to the map when u leave the tab in order for it to be able to be added again.

For example call a method:

public void cleanUp(){
    getFragmentManager().beginTransaction().remove(map).commit();
}

The way I suggest you do it is that you extend mapfragment instead of fragment, then you no longer need a layout file and no extra "tricks" are needed

Zezeq
  • 119
  • 7
  • Sorry, I update my code. I did remove the fragment when the tab is unselected - fragmentTransaction.remove(mFragment). – June Leow Jul 16 '14 at 09:02
  • Marcins answer is a good one and should be followed. But if u want a quick workarround my example code does the trick. If you look closely and compare my code and yours we do it differently and removes different fragments. For the fix you need to remove the map fragment you declared in xml from your fragment. That is what my line of code does. – Zezeq Jul 16 '14 at 09:09
  • I tried with the following code as getFragmentManager return me null: Fragment map2 = mFragment.getFragmentManager().findFragmentById(R.id.map); mFragment.getFragmentManager().beginTransaction().remove(map2); fragmentTransaction.remove(mFragment); I still getting the same error...any idea? – June Leow Jul 16 '14 at 09:36
  • Ah, nvm I missed a commit to the transaction. It work thank you ! But do you mind to tell me more the proper way to do this? If I extend MapFragment instead of Fragment do I still able to do what I did in my code above? – June Leow Jul 16 '14 at 09:42
  • If this answer helped, please mark it as useful :) As I see it there are two ways to do it: Create the map from code like Marcin is suggestin or extend mapfragment directly. If you extend mapfragment you need to come up with another way to have your button that you want, since u can't have a xml file. For instance could you have an activity with a button at the bottom and the rest of the screen is a FrameLayout which you fill with your own mapfragment. – Zezeq Jul 16 '14 at 09:47
  • Sorry I can't do that, I don't have enough reputation. :( – June Leow Jul 16 '14 at 16:40