将片段加载到Android中的另一个片段中

时间:2019-03-31 09:35:22

标签: android android-fragments

我正在尝试从Google Maps SDK实施自动完成功能。

我找到了一个指南,其中它使用片段来显示栏,您可以在其中搜索具有自动填充功能的地址。

当我尝试将其集成到我的代码中时,我的问题就出现了。我有一个被夸大的片段的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"
tools:context=".account.profiles.ProfileTenantActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/browser_actions_bg_grey"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="0dp">

            <Spinner
                android:id="@+id/spinnerSmoking"
                android:layout_width="275dp"
                android:layout_height="40dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:background="@color/common_google_signin_btn_text_light_default"
                android:entries="@array/smoke_friendly"
                android:popupBackground="@color/browser_actions_text_color"
                android:spinnerMode="dropdown"
                app:layout_constraintBottom_toTopOf="@+id/confirm"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.3"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerPetFriendly"
                app:layout_constraintVertical_bias="0.136" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="8dp"
                android:text="@string/looking_for"
                android:textColor="@color/black"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.336"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="What are you looking for?" />

            <android.support.v7.widget.CardView
                android:id="@+id/idCardView"
                android:layout_width="395dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="11dp"
                app:cardCornerRadius="4dp"
                app:layout_constraintBottom_toTopOf="@+id/confirm"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.136">

                <fragment
                    android:id="@+id/autocomplete_fragment"
                    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.v7.widget.CardView>

            <com.itcom202.weroom.account.profiles.SeekBar.BubbleSeekBar
                android:id="@+id/radiusCenter"
                android:layout_width="271dp"
                android:layout_height="44dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="100dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.33"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView8" />

            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="19dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="@string/radius_from_center"
                android:textColor="@color/black"
                android:textSize="14sp"
                app:layout_constraintBottom_toTopOf="@+id/radiusCenter"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.149"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView8"
                app:layout_constraintVertical_bias="0.983" />

            <Spinner
                android:id="@+id/spinnerPeriodRenting"
                android:layout_width="275dp"
                android:layout_height="40dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="44dp"
                android:layout_marginEnd="8dp"
                android:background="@color/common_google_signin_btn_text_light_default"
                android:entries="@array/rending_period_array"
                android:popupBackground="@color/browser_actions_text_color"
                android:spinnerMode="dropdown"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.35"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/radiusCenter" />

            <EditText
                android:id="@+id/depositMin"
                android:layout_width="71dp"
                android:layout_height="39dp"
                android:autofillHints=""
                android:background="@color/common_google_signin_btn_text_light_default"
                android:ems="10"
                android:hint="@string/min"
                android:inputType="number"
                tools:layout_editor_absoluteX="133dp"
                tools:layout_editor_absoluteY="288dp" />

            <EditText
                android:id="@+id/depositMax"
                android:layout_width="71dp"
                android:layout_height="39dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="84dp"
                android:autofillHints=""
                android:background="@color/common_google_signin_btn_text_light_default"
                android:ems="10"
                android:hint="@string/max"
                android:inputType="number"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerPeriodRenting" />

            <EditText
                android:id="@+id/rentMin"
                android:layout_width="71dp"
                android:layout_height="39dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="52dp"
                android:autofillHints=""
                android:background="@color/common_google_signin_btn_text_light_default"
                android:ems="10"
                android:hint="@string/min"
                android:inputType="number"
                app:layout_constraintEnd_toStartOf="@+id/rentMax"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/depositMin" />

            <EditText
                android:id="@+id/rentMax"
                android:layout_width="71dp"
                android:layout_height="39dp"
                android:layout_marginStart="252dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:autofillHints=""
                android:background="@color/common_google_signin_btn_text_light_default"
                android:ems="10"
                android:hint="@string/max"
                android:inputType="number"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.025"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/depositMax" />

            <TextView
                android:id="@+id/textView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="24dp"
                android:layout_marginEnd="8dp"
                android:text="@string/deposit"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toStartOf="@+id/depositMin"
                app:layout_constraintHorizontal_bias="0.777"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerPeriodRenting" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="28dp"
                android:layout_marginEnd="8dp"
                android:text="@string/rent"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toStartOf="@+id/rentMin"
                app:layout_constraintHorizontal_bias="0.827"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView10" />

            <TextView
                android:id="@+id/textView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="8dp"
                android:text="@string/_5km"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toStartOf="@+id/textView13"
                app:layout_constraintHorizontal_bias="0.187"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/radiusCenter" />

            <TextView
                android:id="@+id/textView13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="128dp"
                android:text="@string/_50km"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/radiusCenter" />

            <TextView
                android:id="@+id/textView14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="8dp"
                android:text="@string/preferred_age_of_landlord"
                android:textColor="@color/black"
                android:textSize="14sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.188"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rentMin"
                tools:text="Preferred age of Landlord" />

            <EditText
                android:id="@+id/ageMinL"
                android:layout_width="71dp"
                android:layout_height="39dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="28dp"
                android:autofillHints=""
                android:background="@color/common_google_signin_btn_text_light_default"
                android:ems="10"
                android:hint="@string/min"
                android:inputType="number"
                app:layout_constraintEnd_toStartOf="@+id/ageMaxL"
                app:layout_constraintHorizontal_bias="0.734"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rentMin" />

            <EditText
                android:id="@+id/ageMaxL"
                android:layout_width="71dp"
                android:layout_height="39dp"
                android:layout_marginTop="28dp"
                android:layout_marginEnd="84dp"
                android:autofillHints=""
                android:background="@color/common_google_signin_btn_text_light_default"
                android:ems="10"
                android:hint="@string/max"
                android:inputType="number"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rentMax" />

            <Spinner
                android:id="@+id/spinnerGenderLL"
                android:layout_width="275dp"
                android:layout_height="40dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="12dp"
                android:layout_marginEnd="8dp"
                android:background="@color/common_google_signin_btn_text_light_default"
                android:entries="@array/gender_array"
                android:popupBackground="@color/browser_actions_text_color"
                android:spinnerMode="dropdown"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.341"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ageMinL" />

            <Spinner
                android:id="@+id/spinnerNationalityLL"
                android:layout_width="275dp"
                android:layout_height="40dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="8dp"
                android:background="@color/common_google_signin_btn_text_light_default"
                android:popupBackground="@color/browser_actions_text_color"
                android:spinnerMode="dropdown"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.35"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerGenderLL" />

            <Spinner
                android:id="@+id/spinnerPetFriendly"
                android:layout_width="275dp"
                android:layout_height="40dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="104dp"
                android:layout_marginEnd="8dp"
                android:background="@color/common_google_signin_btn_text_light_default"
                android:entries="@array/pet_friendly"
                android:popupBackground="@color/browser_actions_text_color"
                android:spinnerMode="dropdown"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.3"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerNationalityLL" />

            <CheckBox
                android:id="@+id/furnished"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="8dp"
                android:text="@string/furnished"
                android:textColor="@color/black"
                app:layout_constraintEnd_toStartOf="@+id/handicap"
                app:layout_constraintHorizontal_bias="0.612"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerNationalityLL"
                android:buttonTint="@color/black" />

            <CheckBox
                android:id="@+id/internet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="12dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:text="@string/internet"
                android:textColor="@color/black"
                app:layout_constraintBottom_toTopOf="@+id/spinnerPetFriendly"
                app:layout_constraintEnd_toStartOf="@+id/laundry"
                app:layout_constraintHorizontal_bias="0.544"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/furnished"
                app:layout_constraintVertical_bias="0.343"
                android:buttonTint="@color/black" />

            <CheckBox
                android:id="@+id/handicap"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="92dp"
                android:text="@string/handicap_friendly"
                android:textColor="@color/black"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerNationalityLL"
                android:buttonTint="@color/black" />

            <CheckBox
                android:id="@+id/laundry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:layout_marginEnd="152dp"
                android:layout_marginBottom="8dp"
                android:text="@string/laundry"
                android:textColor="@color/black"
                app:layout_constraintBottom_toTopOf="@+id/spinnerPetFriendly"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/handicap"
                app:layout_constraintVertical_bias="0.375"
                android:buttonTint="@color/black" />

            <Button
                android:id="@+id/confirm"
                android:layout_width="166dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="60dp"
                android:layout_marginEnd="8dp"
                android:background="#FF5A60"
                android:text="@string/confirm"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.497"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spinnerPetFriendly" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/confirm"
                app:srcCompat="@drawable/bottomfield" />

        </android.support.constraint.ConstraintLayout>
    </LinearLayout>
</ScrollView>

此布局呈碎片状膨胀。这是我苦苦挣扎的代码。

public class ProfileTenantFragment extends Fragment {
private static final String TAG = "ProfileTenantFragment";

    // Initialize Places.
    Places.initialize(getApplicationContext(), getString(R.string.google_cloud_api_key));

    // Create a new Places client instance.
    PlacesClient placesClient = Places.createClient(getActivity());

    // Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
            getActivity().getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);



    autocompleteFragment.setTypeFilter(TypeFilter.CITIES);

    // Specify the types of place data to return.
    autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

我的autocompleteFragment始终为null。

2 个答案:

答案 0 :(得分:4)

尝试改用getChildFragmentManager()(并在onViewCreated()生命周期回调或更高版本中使用它)

答案 1 :(得分:0)

您可以尝试使用getChildFragmentManager()。代替getFragmentManager(),只需使用getChildFragmentManager()。除了getChildFragmentManager用于片段中时,它们的工作原理相同。