Adding AutocompleteSupportFragment in Dialogue Fragment

0

I added a AutocompleteSupportFragment in a class that extends Dialogue Fragment. The first time I opened the Dialogue fragment, everything works fine. However, the second time, it crashes. I have tried to use getChildFragmentManager and it still crashes.

My class heading.

public class TaskPopupDialog extends DialogFragment
if(!Places.isInitialized()) {
            Places.initialize(getContext(),apiKey);
        }
        placesClient = Places.createClient(getContext());
        AutocompleteSupportFragment autocompleteSupportFragment = (AutocompleteSupportFragment) getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
        autocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.LAT_LNG, Place.Field.NAME));
        autocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(@NonNull Place place) {
                latLng = place.getLatLng();
                destination = place.getName();
                lat = Double.toString(latLng.latitude);
                lng = Double.toString(latLng.longitude);

                Log.i("PlaceApi", "onPlaceSelected: " + latLng.latitude+ "\n"+latLng.longitude);
            }

            @Override
            public void onError(@NonNull Status status) {

            }
        });

This is my xml file for the dialogue fragment.

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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="350dp"
    android:layout_height="650dp"
    android:layout_gravity="center"
    android:textColor="@color/colorAccentDark"
    android:orientation="vertical"
    android:fontFamily="@font/open_sans_light"
    android:textStyle="normal|bold"
    app:layout_constraintHorizontal_bias="0.5"
    android:background="@drawable/rounded_corners"
    android:paddingLeft="40dp"
    android:paddingBottom="40dp">

    <Button
        android:id="@+id/exitbutton"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:background="@drawable/round_button"
        android:text="x"
        android:textColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/onMyWay"
        android:layout_marginRight="5dp"/>

    <TextView
        android:id="@+id/titletext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Title:"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/onMyWay" />

    <TextView
        android:id="@+id/onMyWay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="OnMyWay"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="40dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="40dp"/>

    <EditText
        android:id="@+id/timestamp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="416dp"
        android:ems="20"
        android:hint="TimeStamp"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.387"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/deliveryCharge"
        android:layout_marginRight="40dp"/>

    <Button
        android:id="@+id/task_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="40dp"
        android:background="@drawable/button_bg_rounded_corners"
        android:ems="13"
        android:text="Submit"
        android:textColor="@color/colorAccent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/typeButton"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/Descriptiontext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="TimeDue:"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/description"
        android:layout_marginRight="40dp"/>

    <EditText
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="13"
        android:hint="Enter Description"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titletext"
        android:layout_marginRight="39dp"/>

    <TextView
        android:id="@+id/DeliveryChargetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text="Delivery Charge"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/timeduebutton"
        android:layout_marginRight="40dp"/>

    <EditText
        android:id="@+id/deliveryCharge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="40dp"
        android:ems="3"
        android:hint="$0.00"
        android:inputType="text|numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeliveryChargetext" />

    <Button
        android:id="@+id/timeduebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="52dp"
        android:layout_marginRight="40dp"
        android:background="@drawable/button_bg_rounded_corners"
        android:ems="10"
        android:text="SELECT TIME"
        android:textColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/timeduetext"
        />

    <TextView
        android:id="@+id/timeduetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:hint="Please enter a valid time"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Descriptiontext"
        android:layout_marginRight="40dp"/>

    <RadioGroup
        android:id="@+id/typeButton"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        tools:layout_editor_absoluteX="160dp"
        tools:layout_editor_absoluteY="206dp"
        android:layout_marginTop="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/deliveryCharge"
        >


        <RadioButton
            android:id="@+id/offerBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Offer"
            android:buttonTint="@color/colorPrimaryDark"
            />

        <RadioButton
            android:id="@+id/requestBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Request"
            android:buttonTint="@color/colorPrimaryDark"
            />

    </RadioGroup>

        <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:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="524dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/typeButton"/>




</androidx.constraintlayout.widget.ConstraintLayout>

This is the error shown in my logcat.

 Process: com.example.onmyway, PID: 32343
    android.view.InflateException: Binary XML file line #186: Binary XML file line #186: Error inflating class fragment
    Caused by: android.view.InflateException: Binary XML file line #186: Error inflating class fragment
    Caused by: java.lang.IllegalArgumentException: Binary XML file line #186: Duplicate id 0x7f0a0046, tag null, or parent id 0xffffffff with another fragment for com.google.android.libraries.places.widget.AutocompleteSupportFragment
        at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3209)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.onmyway.ui.task.TaskPopupDialog.onCreateView(TaskPopupDialog.java:91)
android
android-fragments
google-maps-autocomplete
asked on Stack Overflow Dec 3, 2019 by G.L.

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0