切换android地图片段 - 崩溃

时间:2014-05-21 08:40:50

标签: android android-fragments android-mapview supportmapfragment mapfragment

在我的应用程序中,我有两个选项卡(地图和附近)。两个片段都使用地图片段。

这是我的map.xml

<RelativeLayout
            android:id="@+id/countryLayout"
            android:layout_below="@id/partnersLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/mainFooterLayout">

        <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <fragment
                    android:id="@+id/map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:name="com.google.android.gms.maps.MapFragment"/>

        </FrameLayout>

    </RelativeLayout>

这是我的虚增代码

 View view = inflater.inflate(R.layout.map, container, false);

在NearbyFragment和MyMapFragment中我调用了

 public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
        FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }

但我的应用程序仍然崩溃了

我有这个例外

Caused by: java.lang.IllegalArgumentException: Binary XML file line #42: Duplicate id 0x7f0600b5, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment

有人能告诉我这里有什么问题吗?

我是否可以使用MapFragment而不是SupportMapFragment?

谢谢

1 个答案:

答案 0 :(得分:0)

看看你应该做什么:

1)定义活动布局:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2)为表格式寻呼机创建适配器:

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    List<Fragment> mValues = new ArrayList<Fragment>();

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);

        // HERE YOU ADD INSTANCES OF FRAGMENTS WHAT YOU NEED
        mValues.add(new com.google.android.gms.maps.MapFragment());
        mValues.add(new com.google.android.gms.maps.MapFragment());
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Map";
            case 1:
                return "Nearby";
        }
        return null;
    }
}

3)使用标签创建主要活动

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

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

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

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

}

在内部适配器中,您必须创建需要放置在活动中的碎片网络实例,如标签

P.S。抱歉我的英文和拼写错误