如何使用谷歌地图片段与动作栏标签

时间:2013-11-25 20:42:47

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

我有这个(见下文)活动,其中我有带标签的操作栏。每个标签都是一个片段。其中一个片段用google map v2片段充气。

问题是我无法从夸大的视图中获取GoogleMap对象。

活性:

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

        }
    }
}

片段:

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();)的Cannot cast from Fragment to SupportMapFragment片段中。现在我尝试将此更改为:android.app.Fragment;android.app.support.v4.Fragment;,但它导致MasterActivity出错。我该怎么解决这个???

1 个答案:

答案 0 :(得分:4)

你的主要问题是你正在混合来自不同图书馆的不同类型的片段。

如果您使用SupportMapFragment,则无法使用android.app.Fragment并且需要使用android.app.support.v4.Fragment,则无法解决此问题。如果您在更改为使用支持片段时遇到错误,那么只需修复它们,它可能也与您混合类有关。

下一个问题是MapsFragment需要MapFragmentSupportMapFragment它不能只是Fragment

如果您使用的是原生片段,则无需使用FragmentActivity,您可以使用常规Activity,然后使用MapFragment

总结一旦你使用一个库中的东西,你需要坚持使用该库,你不能将原生片段与支持片段混合