【问题标题】:Binary XML file line #26: Duplicate id, tag null, or parent id with another fragment二进制 XML 文件第 26 行:与另一个片段重复 id、标记 null 或父 id
【发布时间】:2016-06-22 05:53:24
【问题描述】:

我正在尝试将一个片段插入另一个片段,并且我已经成功地做到了这一点,直到我第一次吃午饭主片段,但当我尝试重新加载片段时,应用程序崩溃了,并且我有这个错误:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #26:
Duplicate id 0x7f0e00e2, tag null, or parent id 0xffffffff with another
fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment

这是我的片段

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

View  rootView = inflater.inflate(R.layout.source_destination,container, false);
PlaceAutocompleteFragment sourcr_autocompleteFragment = (PlaceAutocompleteFragment)
          getActivity().getFragmentManager()
.findFragmentById(R.id.sourcr_autocomplete_fragment);
return rootView;

// List item

}

这是我的 XML

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:padding="10dp"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_medium"
            android:layout_marginBottom="@dimen/margin_medium">

            <fragment
                android:id="@+id/sourcr_autocomplete_fragment"    
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"           
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />

        </android.support.v7.widget.CardView>
    </LinearLayout>

【问题讨论】:

  • 你试过这篇文章中的建议了吗? stackoverflow.com/questions/14083950/…
  • 是的,我已经检查过了,但没有用。这里在 xml 中使用片段,所以它不起作用
  • 我终于意识到,不要在片段中使用片段。所以我转移到 PlaceAutocomplete Activity 然后解决了我的问题。谢谢大家的支持

标签: android android-fragments android-nested-fragment


【解决方案1】:

我刚刚遇到了同样的问题,可以通过删除 onDismiss 回调中的片段来解决。

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);

    if (getActivity() != null) {
        FragmentManager fragmentManager = getActivity().getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.sourcr_autocomplete_fragment);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }
}

【讨论】:

    【解决方案2】:

    覆盖片段中的 onDestroyView 方法

    public void onDestroyView() {
        super.onDestroyView();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.sourcr_autocomplete_fragment);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }
    

    科特林:

    override fun onDestroyView() {
        super.onDestroyView()
        activity?.supportFragmentManager?.also { fragmentManager ->
            fragmentManager.findFragmentById(R.id.aboutFragment)?.also { fragment ->
                fragmentManager.beginTransaction().remove(fragment).commit()
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复,我已经尝试过,但没有运气得到同样的错误
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多