2014-03-13 40 views
0

我有一個標籤導航工作(2標籤)。在第二個標籤中,我想插入一個Google地圖對象。Google地圖在Sherlock Tab

代碼使用標籤:

public class FragmentTabsTienda extends SherlockFragmentActivity { 
private static String TAG = "TabActivity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.frag_pdvs); 
    FragmentManager fm = getSupportFragmentManager(); 

    ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    ActionBar.Tab tab1 = actionBar.newTab(); 
    tab1.setText("Lista"); 
    tab1.setIcon(R.drawable.ic_action_view_as_list); 

    ActionBar.Tab tab2 = getSupportActionBar().newTab(); 
    tab2.setText("Mapa"); 
    tab2.setIcon(R.drawable.ic_action_location_map); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setTitle(""); 

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME 
      | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM); 

    // create the two fragments we want to use for display content 

    tab1.setTabListener(new TabListener<ListTiendaTabFragment>(this, 
      "lista", ListTiendaTabFragment.class)); 
    tab2.setTabListener(new TabListener<MapTiendasTabFragment>(this, 
      "mapa", MapTiendasTabFragment.class)); 

    actionBar.addTab(tab1); 
    actionBar.addTab(tab2); 
    // tab2.setTabListener(new TabListener(fMapTiendas)); 
} 

public static class TabListener<T extends Fragment> implements 
     ActionBar.TabListener { 
    private Fragment mFragment; 
    private final Activity 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(Activity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 

    /* The following are each of the ActionBar.TabListener callbacks */ 

    public void onTabSelected(ActionBar.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(ActionBar.Tab tab, FragmentTransaction ft) { 
     if (mFragment != null) { 
      // Detach the fragment, because another one is being attached 
      ft.detach(mFragment); 
     } 
    } 

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { 
     // User selected the already selected tab. Usually do nothing. 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    menu.add("camara").setIcon(R.drawable.ic_action_device_access_camera) 
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 

    menu.add("chat").setIcon(R.drawable.ic_action_social_chat) 
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 

    menu.add("settings").setIcon(R.drawable.ic_settings) 
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); 

    return super.onCreateOptionsMenu(menu); 
} 

} 

在第一個選項卡,我的列表中顯示良好。 但我不能顯示我的地圖:

這是我想要顯示的地圖標籤代碼:

public class MapTiendasTabFragment extends SherlockFragment { 
GoogleMap mMap = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View root = super.onCreateView(inflater, container, savedInstanceState); 

    return root; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    mMap = initMap(); 
} 

private GoogleMap initMap() { 

    if (mMap == null) { 
     FragmentManager fm = getSherlockActivity().getSupportFragmentManager(); 
     SupportMapFragment smf = (SupportMapFragment) fm.findFragmentById(R.id.map); // is null !! 
     mMap = smf.getMap(); 

    } else { 
     mMap.setMyLocationEnabled(true); 
     // Iniciar centrado en Mexico 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
       19.506467, -99.131417), 4)); 
    } 

    return mMap; 
} 
} 

和相關的XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ll_map" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<FrameLayout 
    android:id="@+id/fl_map" 
    android:layout_width="fill_parent" 
    android:layout_height="0.0dip" 
    android:layout_weight="1.0" > 

    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.SupportMapFragment" /> 

    <TextView 
     android:id="@+id/maps_unavailable" 
     style="@style/placeholder_text" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="@string/maps_unavailable" 
     android:visibility="gone" /> 

    <View 
     android:id="@+id/shadow" 
     android:layout_width="fill_parent" 
     android:layout_height="4.0dip" 
     android:layout_gravity="bottom|center" 
     android:background="@drawable/inset_bottom_shadow" /> 

    <ProgressBar 
     android:id="@+id/ab_progress" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:indeterminate="true" 
     android:visibility="gone" /> 
</FrameLayout> 
<!-- 
<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="fill_parent" 
    android:layout_height="@dimen/map_pager_height" /> 
--> 
</LinearLayout> 

在我的代碼, SupportMapFragment smf爲null,我無法獲得任何價值! 任何幫助,將不勝感激

回答

-1

的問題與此要點進行解析: https://gist.github.com/joshdholtz/4522551

public class SomeFragment extends Fragment { 

MapView mapView; 
GoogleMap map; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.some_layout, container, false); 

    // Gets the MapView from the XML layout and creates it 
    mapView = (MapView) v.findViewById(R.id.mapview); 
    mapView.onCreate(savedInstanceState); 

    // Gets to GoogleMap from the MapView and does initialization stuff 
    map = mapView.getMap(); 
    map.getUiSettings().setMyLocationButtonEnabled(false); 
    map.setMyLocationEnabled(true); 

    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls 
    try { 
     MapsInitializer.initialize(this.getActivity()); 
    } catch (GooglePlayServicesNotAvailableException e) { 
     e.printStackTrace(); 
    } 

    // Updates the location and zoom of the MapView 
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10); 
    map.animateCamera(cameraUpdate); 

    return v; 
} 

@Override 
public void onResume() { 
    mapView.onResume(); 
    super.onResume(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mapView.onDestroy(); 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mapView.onLowMemory(); 
} 

} 
+0

鏈接只有答案氣餒,因爲他們可以打破。請將鏈接中的相關信息提取到此答案中,以便即使鏈接不正確,信息仍可用。 – Andy