我正在创建一个包含操作栏和2个标签的应用。像这样:
Tab1:点击后应显示某个地方的一些细节。 Tab2:点击时应显示地图。
我正在使用Fragments。
我的tablistener:
private class MyTabListener implements ActionBar.TabListener {
@Override
public void onTabSelected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
if (tab.getPosition() == 0) {
DetailsFragment fragment = new DetailsFragment();
ft.replace(android.R.id.content, fragment);
} else {
MapsFragment fragment = new MapsFragment();
ft.replace(android.R.id.content, fragment);
//ft.commit();
}
}
MapsFragment.java
public class MapsFragment extends MapFragment {
private MapView map = null;
private MyLocationOverlay me = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return (new FrameLayout(getActivity()));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
map = new MapView(getActivity(),
"0rQAS47Cicu2t7z4w8Ev6A6de7QlbjvKyi8I-QQ");
map.setClickable(true);
map.getController().setCenter(
getPoint(40.76793169992044, -73.98180484771729));
map.getController().setZoom(17);
map.setBuiltInZoomControls(true);
Drawable marker = getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new SitesOverlay(marker));
me = new MyLocationOverlay(getActivity(), map);
map.getOverlays().add(me);
((ViewGroup) getView()).addView(map);
}
@Override
public void onResume() {
super.onResume();
me.enableCompass();
}
@Override
public void onPause() {
super.onPause();
me.disableCompass();
}
private GeoPoint getPoint(double lat, double lon) {
return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}
private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> items = new ArrayList<OverlayItem>();
public SitesOverlay(Drawable marker) {
super(marker);
boundCenterBottom(marker);
items.add(new OverlayItem(getPoint(40.748963847316034,
-73.96807193756104), "UN", "United Nations"));
items.add(new OverlayItem(getPoint(40.76866299974387,
-73.98268461227417), "Lincoln Center",
"Home of Jazz at Lincoln Center"));
items.add(new OverlayItem(getPoint(40.765136435316755,
-73.97989511489868), "Carnegie Hall",
"Where you go with practice, practice, practice"));
items.add(new OverlayItem(getPoint(40.70686417491799,
-74.01572942733765), "The Downtown Club",
"Original home of the Heisman Trophy"));
populate();
}
@Override
protected OverlayItem createItem(int i) {
return (items.get(i));
}
@Override
protected boolean onTap(int i) {
Toast.makeText(getActivity(), items.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return (true);
}
@Override
public int size() {
return (items.size());
}
}
}
问题是当我点击地图标签时出现第一张地图。但是,如果我转到详细信息选项卡并再次返回到地图选项卡,我的应用程序崩溃并出现错误:
logcat的:
12-04 03:28:02.228: E/AndroidRuntime(14466): FATAL EXCEPTION: main
12-04 03:28:02.228: E/AndroidRuntime(14466): java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
12-04 03:28:02.228: E/AndroidRuntime(14466): at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:398)
12-04 03:28:02.228: E/AndroidRuntime(14466): at com.google.android.maps.MapView.<init>(MapView.java:289)
12-04 03:28:02.228: E/AndroidRuntime(14466): at com.google.android.maps.MapView.<init>(MapView.java:235)
12-04 03:28:02.228: E/AndroidRuntime(14466): at com.m7.nomad.MapsFragment.onCreate(MapsFragment.java:34)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.support.v4.app.BackStackRecord.run(Unknown Source)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.os.Handler.handleCallback(Handler.java:725)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.os.Handler.dispatchMessage(Handler.java:92)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.os.Looper.loop(Looper.java:137)
12-04 03:28:02.228: E/AndroidRuntime(14466): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-04 03:28:02.228: E/AndroidRuntime(14466): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 03:28:02.228: E/AndroidRuntime(14466): at java.lang.reflect.Method.invoke(Method.java:511)
12-04 03:28:02.228: E/AndroidRuntime(14466): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-04 03:28:02.228: E/AndroidRuntime(14466): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-04 03:28:02.228: E/AndroidRuntime(14466): at dalvik.system.NativeStart.main(Native Method)
问题是什么我知道MapView不能从片段中膨胀。但我使用这种技术: http://xrigau.wordpress.com/2012/03/22/howto-actionbarsherlock-mapfragment-listfragment/
但这仍然不起作用。我需要一些帮助。
答案 0 :(得分:3)
就在几天前,Google推出了Map Api 2.它包含了MapFragment。所以现在你不需要这样的钩子。 https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment