Estoy desarrollando una app utilizando la api de googlemaps V2 para android
utilizando los actionbars de android 3.x utilizando la biblioteca de compatibilidad con dispositivos 2.x y la libreria de compatibilidad ActionBarSherlock que funcionan 10/10, pero el problema surge cuando intento utilizar los tabs del actionbar para mostrar fragments utilizando un tab con un mapa y otros tabs con contenido normal, la app arranca con normalidad, pero al cambiar de tab y volver a la del mapa o al cambiar de orientacion el telefono cuando el mapa esta seleccionado, muestra un error en tiempo de ejecución, y depurando me doy cuenta que es el método onCreateView del fragment correspondiente al layout del mapa, especificamente en el metodo inflate. Les dejo mi codigo:
Layout Mapa
Código:
La clase del fragment<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Código:
El codigo del activityimport com.example.mapasv2.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapaFragment extends Fragment {
public MapaFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Log.i("DEBUG","Intentado inflar");
View fragment = inflater.inflate(R.layout.layout_mapa, container,false);
//Hasta Aquí no llega la ejecución se cae en la anterior linea
Log.i("DEBUG","Inflo");
return fragment;
}
}
Código:
Esta es la clase que gestiona los eventos del tab que implementa la interface TabListener package com.example.mapasv2;
import android.os.Bundle;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.example.mapasv2.UI.MapaFragment;
import com.example.mapasv2.UI.MiPrimerFragment;
import com.example.mapasv2.UI.TabListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class MapaActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.container);
final ActionBar Action = getSupportActionBar();
Action.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = Action.newTab().setText("Mapa");
TabListener<MapaFragment> tl = new TabListener<MapaFragment>(this,
"Mapa", MapaFragment.class);
tab.setTabListener(tl);
Tab tab2 = Action.newTab().setText("Otro tab");
TabListener<MiPrimerFragment> t2 = new TabListener<MiPrimerFragment>(
this, "Otro tab", MiPrimerFragment.class);
tab2.setTabListener(t2);
Action.addTab(tab);
Action.addTab(tab2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Código:
Basicamente en el metodo attach del evento onTabSelected del tab del mapa se ejecuta el onCreateView del fragment del mapa y da error. import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
*
* @param activity
* The host Activity, used to instantiate the fragment
* @param tag
* The identifier tag for the fragment
* @param clz
* The fragment's Class, used to instantiate the fragment
*/
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
Log.i("DEBUG_TAG","Eliminado");
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}


