错误充气类片段:重复的id,标签为空或与另一个片段具有相同的父id。

10

我正在尝试制作一个应用程序,在该应用程序中,当用户单击ListView中的项目时,将显示GoogleMaps。

我尝试了以下代码:

ShowPlacesActivity.java

public class ShowPlaceActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_place);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container_showplaces, new PlaceholderFragment())
                    .commit();
        }
        getActionBar().setSubtitle(getString(R.string.activity_showplaces_subtitle));
    }

}

activity_show_place.xml

<FrameLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container_showplaces"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.krupal.rememberthisplace.ShowPlaceActivity" tools:ignore="MergeRootFrame" />

fragment_show_place.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"

    >


    <ListView
        android:id="@+id/listView_places"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
      />


</LinearLayout>

fragment_maps.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

我希望在ListView的项单击时显示地图... 因此,我已经实现了以下两个片段:

PlaceholderFragment.java 中,我设置了 onitemclicklistener 如下:

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



            final MySqliteHelper db = new MySqliteHelper(getActivity().getBaseContext());


            listview_places = (ListView)rootView.findViewById(R.id.listView_places);

            final PlacesAdapter places_adapter = new PlacesAdapter(getActivity(),R.layout.listview_item_row,places);


            listview_places.setAdapter(places_adapter);


            listview_places.setLongClickable(true);

            listview_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position,long id) {
                    Place place = places_adapter.getItem(position);
                    String dbvaluename = place.getname();

                  Place selectedplace =  db.getPlace(dbvaluename);


                    dblatitude = selectedplace.getlatitude();

                    dblongitude = selectedplace.getlongitude();


                    dbplacename = place.getname();

                   maps = new MapsFragment();
                    Bundle bundle = new Bundle();
                    bundle.putDouble("dbplacename",dblatitude);
                    bundle.putDouble("dbplacename",dblongitude);
                    bundle.putString("dbplacename",dbplacename);
                   maps.setArguments(bundle);
                    FragmentManager fm = getFragmentManager();

                    FragmentTransaction ft = fm.beginTransaction();

                    ft.replace(R.id.container_showplaces,maps);

                    ft.addToBackStack(null);

                    ft.commit();

                }



        });
}
}

MapsFragment.java 中,我展示了地图:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


   rootView = inflater.inflate(R.layout.fragment_maps, container, false);


    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return rootView;
}

当我第一次点击ListView的项目时,它会显示地图...然后,当我选择另一个项目时,它就会崩溃:

Logcat日志:

android.view.InflateException: 在二进制 XML 文件第 7 行: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) at android.app.BackStackRecord.run(BackStackRecord.java:622) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: 在二进制 XML 文件第 7 行: Duplicate id 0x7f0b001e, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment at android.app.Activity.onCreateView(Activity.java:4248) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032) at android.app.BackStackRecord.run(BackStackRecord.java:622) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) at dalvik.system.NativeStart.main(Native Method)

编辑:这个解决方案对我没有用。


3个回答

26

我现在解决了!

另一种解决方法是链接的答案对我很有帮助。

https://dev59.com/Qeo6XIcBkEYKwwoYOR0q#14484640

如那里所解释,我在包含地图的`fragment`中添加了onDestroyView()

@Override
public void onDestroyView() {
    super.onDestroyView();
    MapFragment f = (MapFragment) getFragmentManager()
                                         .findFragmentById(R.id.mymap);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}

2
伙计,现在早上3点了,我太蠢了,把添加的片段给删除了。谢谢,老兄。干杯... - Jay
这是一个非常好的解决方案,特别是在使用选项卡时。切换到远离当前页面(超出setOffscreenPageLimit())的选项卡将触发Fragment的onDestroyView方法(否则“重复ID问题”将持续存在),但启动另一个活动不会触发该方法,因此当您从启动的活动中返回时,您会发现该片段仍然存在。 - Sotti
3
非常感谢您。非常有效。我困扰了5到6个小时。只需要从片段中删除添加的视图。 - Muhammad Hashim Shafiq

4

1
完美!对我有用。 - Korken55

0
MapFragment f = (MapFragment) getFragmentManager()
                                         .findFragmentById(R.id.mymap);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();

实际上,在onDestroyView内部不需要使用它。我的情况是要删除一个带有SupportMapFragment的已膨胀布局,就这样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接