Foros del Web » Programando para Internet » Android »

Error GoogleMaps V2 + ActionBar tabs

Estas en el tema de Error GoogleMaps V2 + ActionBar tabs en el foro de Android en Foros del Web. Hola tengo un problema que me está volviendo loco, he buscado info en todas partes libros y demás fuentes sin exito, tal vez alguien aquí ...
  #1 (permalink)  
Antiguo 28/12/2012, 10:48
 
Fecha de Ingreso: diciembre-2012
Ubicación: Costa Rica
Mensajes: 16
Antigüedad: 11 años, 3 meses
Puntos: 6
Error GoogleMaps V2 + ActionBar tabs

Hola tengo un problema que me está volviendo loco, he buscado info en todas partes libros y demás fuentes sin exito, tal vez alguien aquí le haya pasado.

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:
<?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" />
La clase del fragment
Código:
import 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;
	}
	
}
El codigo del activity
Código:
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;
	}
}
Esta es la clase que gestiona los eventos del tab que implementa la interface TabListener

Código:
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.
	}
}
Basicamente en el metodo attach del evento onTabSelected del tab del mapa se ejecuta el onCreateView del fragment del mapa y da error.
  #2 (permalink)  
Antiguo 28/12/2012, 11:36
 
Fecha de Ingreso: diciembre-2012
Ubicación: Costa Rica
Mensajes: 16
Antigüedad: 11 años, 3 meses
Puntos: 6
Respuesta: Error GoogleMaps V2 + ActionBar tabs

Acabo ver mi error, y quedó en evidencia mi novatismo en fragments :P, el mapa por si mismo es un fragment, y al parecer anidar fragments no es muy recomendable ya que da error, use la clase del fragment del mapa, SupportMapFragment especificamente y ya el mapFragment se elimina y agrega sin problemas, así:

Tab tab = Action.newTab().setText("Mapa");
TabListener<SupportMapFragment> tl = new TabListener<SupportMapFragment>(this,
"Mapa",SupportMapFragment.class);
tab.setTabListener(tl);

Etiquetas: tabs
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:14.