简体   繁体   中英

how to use google map fragment with action bar tabs

I have this (see below) activity in which I have action bar with tabs. Each tab is a fragment. One of the fragments is inflated with google map v2 fragment.

The problem is that I can't get the GoogleMap object from the inflated View.

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();
    }
}

xml of the map_frag layout:

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

Now in the fragment MapFragment at map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_frag_map_fragment)).getMap(); ( Cannot cast from Fragment to SupportMapFragment ). Now I have tried to change this: android.app.Fragment; to android.app.support.v4.Fragment; but it caused errors in the MasterActivity. How should I solve this???

your main problem is you are mixing different types of fragments from different libraries.

If you are using SupportMapFragment you cannot use android.app.Fragment and need to use android.app.support.v4.Fragment there is no way around this. If you are gatting errors when you change to use support fragment then just fix them and it probably also has to do with you mixing classes.

the next problem is that MapsFragment needs to be either MapFragment or SupportMapFragment it cannot just be a Fragment

if you are using native fragments you dont need to use FragmentActivity and you can use just regular Activity and then use MapFragment

to sum up once you use something from one library you need to stick with that library and you cannot mix native fragments with support fragments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM