Android在片段中添加片段

问题描述 投票:0回答:1

我正在尝试在DialogFragment的子类中添加一个片段。我有一个带有id pref_container的FrameLayout,我想用一个名为settingsFragment的片段替换它。

问题是fragmentTransaction找不到R.id.pref_container,我不知道这是因为pref_container只是在onCreateView中膨胀而不是活动的一部分?

我是Android编程的新手,所以谢谢你的时间。

class QuestionnaireDialog : DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        this.setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme)

    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val view = inflater.inflate(R.layout.questionnaire_layout,container,true)

        val settingsFragment = ExercisesOnlySettingsFragment()
        val ft = fragmentManager?.beginTransaction()

        /// **** Here fragmentTransaction can't find R.id.pref_container because it's inflated later and not loaded as part of the activity? ****

        ft?.add(R.id.pref_container, settingsFragment)
        ft?.commit()

        return view
    }


}

我得到的运行时错误是

No view found for id 0x7f09009c (com.lescadeaux.kegel:id/pref_container) for fragment ExercisesOnlySettingsFragment{4ba4b89 #1 id=0x7f09009c}

这是我的问卷布局XML的相关部分

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:app="http://schemas.android.com/apk/res-auto"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent"
                                             android:id="@+id/questionnaireLayout"
                                             android:background="@android:color/background_light">

    <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView"
            app:layout_constraintTop_toBottomOf="@+id/textView5"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/button"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp">

        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">

            <!-- RELAVENT PART -->
            <FrameLayout
                    android:layout_width="300dp"
                    android:layout_height="match_parent"
                    android:id="@+id/pref_container">
            </FrameLayout>
            <!-- RELAVENT PART -->

        </LinearLayout>
    </HorizontalScrollView>


</android.support.constraint.ConstraintLayout>

android android-fragments kotlin fragment fragmenttransaction
1个回答
2
投票

请尝试使用getChildFragmentManager()(或其kotlin等效物)而不是您正在使用的getFragmentManager() - 它会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.