二进制XML文件(Android)导致的IllegalArgumentException

时间:2014-12-25 10:26:15

标签: java android xml

我有一个带有3个标签的基本导航。其中一个包含一个带谷歌地图的片段。但是导航效果不好,我第二次选择标签后就会崩溃。

它说这是因为地图的XML文件在我看来没有任何错误。任何人都可以看到我现在被蒙蔽的错误吗?

Logcat:

android.view.InflateException: Binary XML file line #7: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.example.dell.animalz.MapsFragment.onCreateView(MapsFragment.java:22)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
        at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1297)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
        at      

android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at   
android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
        at     
android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
        at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
        at com.example.dell.animalz.HomeActivity.onTabSelected(HomeActivity.java:67)
        at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:674)
        at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1217)
        at     
com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:976)
        at android.view.View.performClick(View.java:4637)
        at android.view.View$PerformClick.run(View.java:19422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f070067, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)

HomeActivity.java

public class HomeActivity extends FragmentActivity implements ActionBar.TabListener {

ViewPager mViewPager = null;
private TabsMenuAdapter mAdapter;
private ActionBar actionBar;

private String []tabs = {"News", "Animalz", "Maps"};

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

    mViewPager = (ViewPager)findViewById(R.id.viewpager1);
    actionBar = getActionBar();
    mAdapter = new TabsMenuAdapter(getSupportFragmentManager());

    mViewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for(String tab_names : tabs){
        actionBar.addTab(actionBar.newTab().setText(tab_names).setTabListener(this));
    }
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}

@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) {
    mViewPager.setCurrentItem(tab.getPosition());
}
}

Maps.java

public class MapsFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.maps_layout, container, false);
    return rootView;

  }
}

Maps.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

任何帮助将不胜感激,提前谢谢!

2 个答案:

答案 0 :(得分:0)

您必须在包含地图的片段的onDestroyView()方法中删除MapFragment,并引用this answer,您必须执行以下操作:

// override the onDestroyView of the fragment
@Override
public void onDestroyView() {
    super.onDestroyView();
    MapFragment f = (MapFragment) getFragmentManager()
                                         .findFragmentById(R.id.the_map);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}

答案 1 :(得分:0)

每次检查您的父视图是否为空。如果是,则删除。更改 Maps.java

private static View view;    
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup group,Bundle bundle)
{
    if(view!=null)
    {
        ViewGroup parent =(ViewGroup)view.getParent();
        if(parent!=null)
            parent.removeView(view);
    }
    try{
        view=inflater.inflate(R.layout.maps, group, false);
        ........

    }
    catch(Exception e)
    {
        Log.e("ERROR", ""+e);
    }

    return view;
}