Android 夏洛克标签中的谷歌地图
我有一个标签导航工作(2个标签)。在第二个选项卡中,我想插入一个GoogleMap对象 使用选项卡的代码:Android 夏洛克标签中的谷歌地图,android,google-maps,tabs,actionbarsherlock,Android,Google Maps,Tabs,Actionbarsherlock,我有一个标签导航工作(2个标签)。在第二个选项卡中,我想插入一个GoogleMap对象 使用选项卡的代码: public class FragmentTabsTienda extends SherlockFragmentActivity { private static String TAG = "TabActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav
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);
}
}
以及相关的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为空,我无法获得任何值!!!
如果有任何帮助,我们将不胜感激。根据以下要点解决了问题:
仅链接的答案不鼓励使用,因为它们可能会中断。请将链接中的相关信息拉入此答案,以便即使链接不可用,信息仍然可用。
<?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>
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();
}
}