0

这是一个类似的问题:MapFragment in Action Bar Tabs,但我没有明确的答案。我是 Android 新手,这是我正在开发的第一个应用程序。

我目前有一个MapFragment为 Google Maps v2 创建的 MainActivity。并在 ActionBar 下显示地图,如此屏幕截图所示

我的目标是为 Eclipse 中的 MainActivity 实现选项卡(不使用FragmentActivitys 或我的应用程序的支持库),它当前为 Google Maps v2minSdkVersion=11创建了一个。MapFragment

MainActivity.java 片段

public class MainActivityextends Activity 
implements LocationListener, LocationSource, OnMarkerClickListener, OnInfoWindowClickListener {
    private static GoogleMap googleMap;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map); 
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); //Change to Tab mode 
        ....
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();


地图.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include 
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    layout="@layout/map_fragment" />
</LinearLayout>


map_fragment.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="53.78513542767"
map:cameraTargetLng="-3.14948167651"
map:cameraZoom="14" />


Eclipse 使用支持库为您设置了一个 Activity 选项卡,因此在使用 a 时它给了我一个错误(如下),MapFragment而我又恢复为不使用选项卡。我认为原因在这里

Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment

我需要使用标签,因为目前只开发了一个页面/活动,我需要一种向用户和地图显示过滤器的方法。用户将切换到相邻的选项卡以编辑过滤器,并能够切换回地图选项卡,然后将在其中拾取过滤器并更改地图上的标记。

这么短的问题是否有一种简单的方法可以为我的MainActivity.java实现选项卡?如果不清楚,请询问我的具体情况,但这对我来说非常先进

4

1 回答 1

0

尝试使用 SupportMapFragment 而不是 MapFragment:

地图.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="53.78513542767"
map:cameraTargetLng="-3.14948167651"
map:cameraZoom="14" />
于 2013-12-10T21:24:29.573 回答