我试图显示包含DialogFragmentfragment

考虑以下:

我有一个DialogFragment EntryFragment,里面有一个textview。单击textview时,我试图打开另一个DialogFragment PopUpFragment,但出现错误。

错误日志:

12-03 14:08:49.527: E/AndroidRuntime(3610): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0xffffffff, tag dialog, or parent id 0x0 with another fragment for com.savior.main.ContainerFragment
12-03 14:08:49.527: E/AndroidRuntime(3610): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
12-03 14:08:49.527: E/AndroidRuntime(3610): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)


请注意,PopUpFragment包含一个片段ContainerFragment

popupfragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:padding = "10dp"
             android:tag="dialog"
             class="com.savior.main.ContainerFragment" />

</LinearLayout>


单击textview时,使用此代码库调用PopUpFragment.java,

PopUpFragment cf = new PopUpFragment().newInstace();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("FIELDS", fields);
bundle.putString("LINK-UUID", uuid);
cf.setArguments(bundle);
cf.show(getSupportFragmentManager(), "dialog");


这是我实际的PopUpFragment.java相关代码,

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

    dbAdapter = SaviorDbAdapter.getInstance(getActivity().getApplicationContext());
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(getContentView());
        Bundle bundle = getArguments();
    fields = bundle.getParcelableArrayList("FIELDS");
        uuid = bundle.getString("uuid");
        dialog = builder.create();
    return dialog;
}


private View getContentView() {

        LayoutInflater inflater = getActivity().getLayoutInflater();

        view = inflater.inflate(R.layout.popupfragment, null);

           return view;
}

最佳答案

为了解决这个问题,我做了两个更改:
在片段类中将根视图声明为静态

protected static View rootView = null;

在onCreateView方法中检查rootView是否为null
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
        if( rootView == null )
            rootView = super.onCreateView( inflater, container, savedInstanceState );

        if( rootView != null )
            setUpMap();
}

我希望此解决方案可以为您提供帮助。

10-07 20:41