在我的应用程序中,我有两个标签(地图和附近)。两个片段都在使用map fragment。
这是我的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
有人能告诉我这里怎么了吗?
我是否可以使用mappfragment而不是supportmappfragment?
谢谢你
最佳答案
看看你该怎么做:
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) {
}
}
在适配器中,您必须创建fragmnets的实例,您需要将其放置在活动中,如选项卡
对不起,我的英文和打字错误