java - 如何将谷歌地图 fragment 与操作栏选项卡一起使用

标签 java android google-maps android-layout android-fragments

我有这个(见下文) Activity ,其中有带有选项卡的操作栏。每个选项卡都是一个 fragment 。其中一个 fragment 用谷歌地图 v2 fragment 进行了膨胀。

问题是我无法从膨胀的 View 中获取 GoogleMap 对象。

Activity :

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import com.events.fragments.FragmentTab1;
import com.events.fragments.MapsFragment;
import com.events.fragments.FragmentTab3;
import com.events.activities.R;

public class MasterActivity extends FragmentActivity
{
    // Declare Tab Variable
    ActionBar.Tab Tab1, Tab2, Tab3;
    Fragment fragmentTab1 = new FragmentTab1();
    Fragment fragmentTab2 = new MapsFragment();
    Fragment fragmentTab3 = new FragmentTab3();

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master);

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setText("tab1");
        Tab2 = actionBar.newTab().setText("Tab2");
        Tab3 = actionBar.newTab().setText("Tab3");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        Tab3.setTabListener(new TabListener(fragmentTab3));

        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);
        actionBar.addTab(Tab3);

    }

    public class TabListener implements ActionBar.TabListener {

        Fragment fragment;

        public TabListener(Fragment fragment) {
            // TODO Auto-generated constructor stub
            this.fragment = fragment;
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            ft.replace(R.id.fragment_container, fragment);
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            ft.remove(fragment);
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }
    }
}

fragment :

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Fragment;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import com.events.activities.R;
import com.events.objects.PlaceAutosuggest;
import com.events.objects.PlaceObj;
import com.events.si.PlacesService;
import com.events.views.PlacesAutoSuggAdapter;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MapsFragment extends Fragment
{   
    View rootView;
    GoogleMap map;

    AutoCompleteTextView addressInput;
    ProgressDialog progressDialog;

    FindMapLocationTask findMapLocationTask;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {

        if (rootView != null) 
        {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }
        try 
        {
            rootView = inflater.inflate(R.layout.map_frag, container, false);
        } 
        catch (InflateException e) 
        {
            return rootView;
        }

        addressInput = (AutoCompleteTextView) rootView.findViewById(R.id.map_frag_location_AutoCompleteTextView);
         map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_frag_map_fragment)).getMap();

        addressInput.setAdapter(new PlacesAutoSuggAdapter(getActivity()));
        addressInput.setOnItemClickListener( new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
            {
                PlaceAutosuggest place = (PlaceAutosuggest) adapterView.getItemAtPosition(position);
                addressInput.setText(place.getName());

                findMapLocationTask = new FindMapLocationTask();
                findMapLocationTask.execute(place.getReferance());

                //Toast.makeText(getActivity(), place.getReferance(), Toast.LENGTH_LONG).show();
            }
        });

        return rootView;
    }

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

map_frag 布局的 xml:

<RelativeLayout 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" >

    <AutoCompleteTextView
        android:id="@+id/map_frag_location_AutoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </AutoCompleteTextView>

    <fragment
        android:id="@+id/map_frag_map_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/map_frag_location_AutoCompleteTextView"
        android:layout_alignParentLeft="true"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

现在在 MapFragmentmap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_frag_map_fragment)).getMap(); (无法从 Fragment 转换为 SupportMapFragment)。现在我尝试将其更改为: android.app.Fragment;android.app.support.v4.Fragment; 但它导致了 MasterActivity 中的错误。我该怎么解决这个问题???

最佳答案

您的主要问题是您正在混合来自不同库的不同类型的 fragment 。

如果您使用SupportMapFragment,则无法使用android.app.Fragment,需要使用android.app.support.v4.Fragment这是没有办法解决的。如果您在更改为使用支持 fragment 时遇到错误,那么只需修复它们,它可能也与您混合类有关。

下一个问题是 MapsFragment 必须是 MapFragmentSupportMapFragment 它不能只是一个 Fragment

如果您使用 native fragment ,则不需要使用 FragmentActivity,您可以仅使用常规 Activity,然后使用 MapFragment

总而言之,一旦您使用某个库中的某些内容,您就需要坚持使用该库,并且不能将 native fragment 与支持 fragment 混合使用

关于java - 如何将谷歌地图 fragment 与操作栏选项卡一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20202887/

相关文章:

android - 按钮应该粘在底部,带有 adjustResize

ios - 在 iOS 中使用 Google map 时如何使用自定义标注进行注释?

java - 日期格式图片在转换整个输入字符串之前结束

java - 如何在 .properties 文件中引用另一个项目

Android Eclipse 使用外部工具在代码上运行脚本

android - 不要剪辑 ViewPager 页面

google-maps - 使用 Google Maps API 查找最近点

javascript - 隐藏使用 Google map 绘图库绘制的多边形

java - 如何在文本文件中*在*特定的其他点之后搜索一个点?

java - blat 可以向其发送电子邮件的收件人数量是否有限制