我有这个(见下文)活动,其中有带有选项卡的操作栏。每个选项卡都是一个片段。片段之一是通过Google Map v2片段膨胀的。

问题是我无法从膨胀的View中获取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中引起了错误。我该如何解决呢???

最佳答案

您的主要问题是要混合来自不同库的不同类型的片段。

如果使用的是SupportMapFragment,则不能使用android.app.Fragment,而需要使用android.app.support.v4.Fragment,因此无法解决此问题。如果在更改使用支持片段时遇到错误,则只需修复它们,这可能还与您混合类有关。

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

如果您使用的是本机片段,则无需使用FragmentActivity,而可以只使用常规的Activity,然后使用MapFragment

总结一下,一旦您使用了一个库中的某些内容,就需要坚持使用该库,并且不能将本机片段与支持片段混合使用

10-07 19:37
查看更多