【问题标题】:How to use mapfragment in a fragment如何在片段中使用 mapfragment
【发布时间】:2013-04-15 13:05:25
【问题描述】:

主要是我有一个带有下拉菜单的 FragmentActivity,一旦您从该菜单中选择一个项目,它就会使我的私有类扩展 Fragment 以在地图上显示它自己的其他东西 现在的问题是,当我在控制地图的片段类中划线时:

GoogleMap map = ((MapFragment)getFragmentManager.findFragmentById(R.id.map)).getMap()

它说你不能将片段转换为 MapFragment,所以我尝试为该类扩展 MapFragment,但是当我在下拉菜单中选择一个项目时,我会执行这部分代码:

Fragment fragment = new MyFragmentClass()();
Bundle args = new Bundle();
args.putInt(PlacesFinder.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();

我只能使用片段,所以如果我无法在片段类中找到查看它并且由于下拉菜单而无法扩展地图片段,我该如何使用片段中的谷歌地图?

谢谢提醒:)

【问题讨论】:

  • 无法理解您的问题。问题是在片段中使用MapFragment 吗?
  • 问题是我想在片段中使用 GoogleMap 对象,但我需要使用 MapFragment 因为我得到了片段的 lyaout:schemas.android.com/apk/res/android" android :id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.MapFragment"/> 我使用但我不知道如何在片段类中将其与 MapFragment 相关联

标签: android fragment google-maps-android-api-2 mapfragment


【解决方案1】:

您可以在片段中使用GoogleMap。仅查看在 XML 中创建片段的代码部分就会出现错误。假设这个片段是你的 XML 布局中唯一的元素,它应该是这样读取的:

<fragment xmlns:android="schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="match_parent"      
    android:layout_height="match_parent"  
    class="com.google.android.gms.maps.MapFragment"/>

你的 XML 的最后一行,你有 android:name 是错误的。您在活动中添加地图的代码应该可以工作

GoogleMap map = ((MapFragment)getFragmentManager.findFragmentById(R.id.map)).getMap();

我之前用过下面这段代码没有问题

mapFrag = ((SupportMapFragment) getFragmenManager().findFragmenById(R.id.map)).getMap();

唯一的区别是(1)我使用的是SupportMapFragment; (2) 我在onCreate 之外声明了我的GoogleMap

这听起来是基于您所描述的片段无法转换为正确的类,这是片段 XML 中的错误或您没有使用正确的导入的问题。如果您使用的是MapFragment,那么您需要确保您使用的是MapFragment 导入(不是SupportMapFragment 或只是Fragment)。

至于代码的最后一部分,我不确定与下拉菜单的关系是什么。看起来您正在将参数传递给片段?如果您可以提供更多详细信息,我也会对此发表评论。

【讨论】:

    【解决方案2】:

    抱歉耽搁了,但这是主要活动:

    public class MainActivity extends FragmentActivity implements
        ActionBar.OnNavigationListener , LocationListener{
        private String [] types;
        private ActionBar actionBar;
    
        public static GoogleMap map = null;
        public static boolean googlePlayOn = false;
        private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            types = getResources().getStringArray(R.array.values);
            googlePlayOn = isGooglePlay();
            actionBar = getActionBar();
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    
            // Set up the dropdown list navigation in the action bar.
            actionBar.setListNavigationCallbacks(
            // Specify a SpinnerAdapter to populate the dropdown list.
                    new ArrayAdapter<String>(getActionBarThemedContextCompat(),android.R.layout.simple_list_item_1,android.R.id.text1, types), this);
        }
    
        /**
         * This function checks in the phone operating system that 
         * the phone got google play services
         * @return
         */
        private boolean isGooglePlay()
        {
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if(status == ConnectionResult.SUCCESS)
                return true;
            else
                ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
            return false;
        }
    
    
        @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        private Context getActionBarThemedContextCompat() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                return getActionBar().getThemedContext();
            } else {
                return this;
            }
        }
    
        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
            // Restore the previously serialized current dropdown position.
            if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
                getActionBar().setSelectedNavigationItem(
                        savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
            }
        }
    
        @Override
        public void onSaveInstanceState(Bundle outState) {
            // Serialize the current dropdown position.
            outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
                    .getSelectedNavigationIndex());
        }
    
    
        @Override
        public boolean onNavigationItemSelected(int position, long id) {
            // When the given dropdown item is selected, show its contents in the
            // container view.
            if(position != PlacesFinder.currentTitle)
            {
                if(!PlacesFinder.created)
                {
                    PlacesFinder.created = true;
                    Bundle b = new Bundle();
                    b.putInt(PlacesFinder.ARG_SECTION_NUMBER, position);
                    Fragment fragment = new PlacesFinder();
                    fragment.setArguments(b);
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
                }
                PlacesFinder.newTitleBeenChosenHandler.sendEmptyMessage(0);
            }
            return true;
        }
    
    
    
        /**
         * A dummy fragment representing a section of the app, but that simply
         * displays dummy text.
         */
        public static class PlacesFinder extends Fragment {
            /**
             * The fragment argument representing the section number for this
             * fragment.
             */
            public static boolean created = false;
            public static final String ARG_SECTION_NUMBER = "section_number";
            public static Handler newTitleBeenChosenHandler;
            public static int currentTitle = 0;
            private Context context;
            public PlacesFinder() {
                this.context = getActivity();
                loadMapFragment();
            }
            private void loadMapFragment()
            {
                if(googlePlayOn)
                {
                    if(map == null)
                        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    
                }
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                return inflater.inflate(R.layout.places_fragment, container , false);
            }
    
    
        }
    
    
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
    
        }
    
    }
    
    
    
    
    This is the main xml file :
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        tools:ignore="MergeRootFrame" />
    

    这是地图片段文件:

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:name="com.google.android.gms.maps.MapFragment"/>
    

    一旦我做了这行代码 map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    应用程序失败,它说我有一个空指针,但我不知道为什么......

    【讨论】:

      【解决方案3】:

      您应该使用 getSupportFragmentManager()。

      map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
      

      当您使用 FragmentActivity 时,这应该可以工作。尽管我正在使用 Fragment 并且不会这样做,但我也面临同样的问题:(

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多