Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向对话框中添加片段_Android_Android Fragments_Android Dialogfragment_Android Nested Fragment - Fatal编程技术网

Android 向对话框中添加片段

Android 向对话框中添加片段,android,android-fragments,android-dialogfragment,android-nested-fragment,Android,Android Fragments,Android Dialogfragment,Android Nested Fragment,我想在对话框中添加一个片段(可以是DialogFragment,也可以是常规对话框)。我该怎么做 这是我的对话片段: public class MyDialogFragment extends DialogFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { MyDialogFragment2 dialog

我想在对话框中添加一个片段(可以是DialogFragment,也可以是常规对话框)。我该怎么做

这是我的对话片段:

public class MyDialogFragment extends DialogFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MyDialogFragment2 dialog = new MyDialogFragment2();
        View v = inflater.inflate(R.layout.news_articles, container, false);
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, dialog).commit();
        return v;
    }

}
以下是news_article.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
但当我尝试时,我得到:

No view found for id 0x7f070002 for fragment MyDialogFragment2
我认为这是因为活动的FragmentManager不是我应该添加的,但我找不到DialogFragment的FragmentManager,它在哪里?

答案(感谢@Luksprog)是使用getChildFragmentManager而不是getActivity()。getSupportFragmentManager


它不适用于我,因为我必须升级support-v4 jar,如下所述:

对话框布局-R.Layout.view\u with\u plus

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.util.me.TestActivity"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Details"/>
    <fragment
        android:layout_toRightOf="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email"
        class="com.util.me.test.PlusOneFragment"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

class=“com.util.me.test.PlusOneFragment”中的片段只是Android Studio生成的PlusOneFragment

getChildFragmentManager().beginTransaction()…
谢谢,但这只适用于API 17,不是吗?对于本机片段,是的,因为它们是从4.2引入的。但是您始终可以选择来自支持兼容性包的片段,该包使用相同的
getChildFragmentManager()
方法。。。找不到它,因为有一个旧的v4版本。。。找到这个:,谢谢!注意:当您多次显示对话框时,这可能会有一些问题。静态膨胀片段将把片段添加到对话框的ownerActivity的fragmentManager中,当您关闭对话框时,片段仍然存在。所以下次充气时,将抛出异常。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.util.me.TestActivity"
    >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Details"/>
    <fragment
        android:layout_toRightOf="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/email"
        class="com.util.me.test.PlusOneFragment"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
public void showDialog(View vIew){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    View view = this.getLayoutInflater().inflate(R.layout.view_with_plus, null);
    builder.setView(view)
            .setPositiveButton("OK", null)
            .setNegativeButton("Cancel", null);

    AlertDialog dialog = builder.create();
    dialog.show();
}