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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With