SupportPlaceAutocompleteFragment抛出"复制id,tag null或parent id与com..places.ui.SupportPlaceAutocompleteFragment的另一个片段

时间:2017-04-11 21:22:12

标签: android android-fragments google-places-api google-places

所以,我在一个片段(tab)中显示一个Map(使用MapView)并且它工作得非常好,但是我在选项卡的xml中添加了SupportMapFragment,在我尝试重新选择选项卡后它崩溃了。以下是崩溃的完整日志:

04-11 23:27:04.972 15121-15121/com.cynicalpolitcal.muana E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.cynicalpolitcal.muana, PID: 15121
                                                                       android.view.InflateException: Binary XML file line #46: Binary XML file line #46: Error inflating class fragment
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
                                                                           at com.cynicalpolitcal.muana.TabExplore.onCreateView(TabExplore.java:68)
                                                                           at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                           at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:753)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                           at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                           at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984)
                                                                           at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
                                                                           at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                           at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                                                                           at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
                                                                           at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
                                                                           at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
                                                                           at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2191)
                                                                           at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1164)
                                                                           at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1157)
                                                                           at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1127)
                                                                           at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1426)
                                                                           at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1536)
                                                                           at android.view.View$PerformClick.run(View.java:22596)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                        Caused by: android.view.InflateException: Binary XML file line #46: Error inflating class fragment
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
                                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
                                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
                                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
                                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                           at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
                                                                           at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                                                                           at com.cynicalpolitcal.muana.TabExplore.onCreateView(TabExplore.java:68) 
                                                                           at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) 
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 
                                                                           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
                                                                           at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:753) 
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 
                                                                           at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 
                                                                           at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984) 
                                                                           at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626) 
                                                                           at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) 
                                                                           at android.support.v4.view.ViewPager.populate(ViewPager.java:1268) 
                                                                           at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668) 
                                                                           at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630) 
                                                                           at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611) 
                                                                           at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2191) 
                                                                           at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1164) 
                                                                           at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1157) 
                                                                           at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1127) 
                                                                           at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1426) 
                                                                           at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1536) 
                                                                           at android.view.View$PerformClick.run(View.java:22596) 
                                                                           at android.os.Handler.handleCallback(Handler.java:739) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:7325) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                        Caused by: java.lang.IllegalArgumentException: Binary XML file line #46: Duplicate id 0x7f0f00f5, tag null, or parent id 0x7f0f00f4 with another fragment for com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment
                                                                           at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3447)
                                                                           at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                                        at android.view.LayoutInflater

我是第一次打开应用程序,它运行正常,直到我尝试重新加载选项卡。 Here is the first tab opened - it is okIt crashes after this.

片段在协调器布局中,第一次打开时第一次就可以正常工作。

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarlayout_explore"
    android:layout_width="match_parent"

    app:layout_scrollFlags="scroll|enterAlways"

    android:layout_height="wrap_content">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_bar_in_layout"
        android:layout_width="wrap_content"
        app:titleEnabled="false"
        app:statusBarScrim="@null"
        app:layout_scrollFlags="scroll|enterAlways"

        android:fitsSystemWindows="true"
        android:layout_height="400dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:enabled="true"
            android:focusable="true"
            android:clickable="true"
            app:layout_collapseMode="parallax"
            android:layout_height="400dp">
        <com.google.android.gms.maps.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="420dp" />
       <LinearLayout
            android:layout_margin="16dp"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:orientation="horizontal"
            android:id="@+id/titleView"
            android:elevation="0dp"
            android:layout_alignTop="@id/mapView"
            android:background="@color/tw__solid_white">

           <fragment
               android:id="@+id/place_autocomplete_fragment"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
               android:layout_below="@id/titleView"/>
   </LinearLayout>

        </RelativeLayout>
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>


<android.support.v7.widget.RecyclerView
    android:layout_gravity="bottom"
    android:layout_below="@id/titleView"
    android:id="@+id/listofusers"
    android:elevation="11dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    app:layout_collapseMode="parallax"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

以下是tab.java中与片段有关的全部代码:

SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
          getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            googleMap.clear();
            CameraPosition cameraPosition = new CameraPosition.Builder().target(place.getLatLng()).zoom(11).build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            googleMap.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName().toString()));

            Toast.makeText(getContext(), place.getClass().getName().toString(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i("LOGINCAZDEEROARE:", "Error happened: " + status);
            Toast.makeText(getContext(), "IT DID NOT WORK", Toast.LENGTH_SHORT).show();
        }
    });

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,希望这对某人有所帮助! 有人和我有类似的问题,但有一个地图片段。 This answer为我工作。

答案如下

  

如果n是标签数量,那么:

     

mViewPager.setOffscreenPageLimit(N);

     

查看寻呼机实现队列   所以,你不必让它删除那个片段。 onCreateView是   只召唤一次。

注意:当布局包含a时,您无法将布局扩展为片段。仅在动态添加到片段时才支持嵌套片段

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment