Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentMap + ActionBar Tab

I've been trying insert a MapView into an ActionBar Tab, but I wasn't able to solve the problem even googling.

Here is the Main Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.action_bar_tabs); 
    FragmentManager fm = getSupportFragmentManager();
    fm.beginTransaction().add(android.R.id.content, GigLoader.GigLoaderListFragment.newInstance()).commit();

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = getSupportActionBar().newTab().setText("Geo").setTabListener(this);
    ActionBar.Tab tab2 = getSupportActionBar().newTab().setText("Lista").setTabListener(this);
    getSupportActionBar().addTab(tab1);
    getSupportActionBar().addTab(tab2);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    if (tab.getPosition() == 0) {
        fm.beginTransaction().add(android.R.id.content, GigLoader.GigLoaderListFragment.newInstance()).commit();
    }
    else {
        fm.beginTransaction().add(android.R.id.content, GeoGigLoader.GeoGigMapFragment.newInstance()).commit();
    }

}

And here the code of the GeoGigLoader:

public class GeoGigLoader extends FragmentMapActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
}

public static final class GeoGigMapFragment extends Fragment {

    static GeoGigMapFragment newInstance() {
        GeoGigMapFragment map = new GeoGigMapFragment();
        return map;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = getActivity().getLayoutInflater().inflate(R.layout.map_gigs, container, false);
        MapView mapView = (MapView)view.findViewById(R.id.map_view);
        mapView.setBuiltInZoomControls(true);
        return view;
    }

}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

FragmentMapActivity is a library from actionbarsherlock.com, and this one extends from a MapActivity, so it should work.

The error I get, is the next one:

FATAL EXCEPTION: main E/AndroidRuntime(954): android.view.InflateException: Binary XML file line #2: Error inflating class com.google.android.maps.MapView E/AndroidRuntime(954): at android.view.LayoutInflater.createView(LayoutInflater.java:513) E/AndroidRuntime(954): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)

Anybody know what's happening?

like image 365
cesards Avatar asked Nov 16 '11 15:11

cesards


1 Answers

Solution is to start mapview in the main activity which should extend FragmentMapActivity. Then your fragment can implement the mapview by getting it from the main activity.

Main activity:

    mMapView = new MapView(YourActivity.this, MAPS_KEY);
    mMapView.setClickable(true);
    Exchanger.mMapView = mMapView;

Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mMapView = Exchanger.mMapView;
    return mMapView;
}

@Override
public void onDestroy() {
    if(mMapView != null) {
        NoSaveStateFrameLayout parentView = (NoSaveStateFrameLayout) mMapView.getParent();
        parentView.removeView(mMapView);
    }
    super.onDestroy();
}

Exchanger is a class with a static field with mapview in it.

like image 135
Warpzit Avatar answered Sep 24 '22 09:09

Warpzit