我有一个标签导航工作(2个标签)。在第二个标签中,我想插入一个谷歌地图对象。
使用标签的代码:
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,我无法获得任何值! 任何帮助将不胜感激
答案 0 :(得分:-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();
}
}