0
votes

I have a class to create the dialog and coding to get the values from it. it works fine for one. and when i try to call dialog for the second time it passes following error message.

public class DriverUpdateStatus extends DialogFragment {
    private static DriverUpdateStatus driverUpdateStatus;
    private Context context;
    private View view;

    public static DriverUpdateStatus newInstance(Context context) {

        if (driverUpdateStatus == null) {
            driverUpdateStatus = new DriverUpdateStatus();
            driverUpdateStatus.view = LayoutInflater.from(context).inflate(R.layout.dialog_driver_update_status, null);
        }
        driverUpdateStatus.context = context;
        return driverUpdateStatus;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(context)
                .setTitle("SELECT_STATE")
                .setView(this.view)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                    }
                })
                .create();
    }
}

<?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">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"

        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/im_off_duty"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:backgroundTint="@color/cardview_light_background"
                android:src="@drawable/ic_power_settings_new_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Off Duty" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/im_on_duty"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:backgroundTint="@color/cardview_light_background"
                android:src="@drawable/ic_check_circle_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="On Duty" />
        </LinearLayout>
    </LinearLayout>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <ImageButton
                android:id="@+id/im_sleeper"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:backgroundTint="@color/cardview_light_background"

                android:src="@drawable/ic_airline_seat_individual_suite_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Sleeper" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:orientation="vertical">

            <ImageButton
                android:id="@+id/im_driving"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:backgroundTint="@color/cardview_light_background"

                android:src="@drawable/ic_do_not_disturb_on_black_24dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Driving" />
        </LinearLayout>
    </LinearLayout>

Process: com.jackfruitsystems.fleetoperate, PID: 4759 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4310) at android.view.ViewGroup.addView(ViewGroup.java:4146) at android.view.ViewGroup.addView(ViewGroup.java:4118) at com.android.internal.app.AlertController.setupCustomContent(AlertController.java:585) at com.android.internal.app.AlertController.setupView(AlertController.java:496) at com.android.internal.app.AlertController.installContent(AlertController.java:234) at android.app.AlertDialog.onCreate(AlertDialog.java:423) at android.app.Dialog.dispatchOnCreate(Dialog.java:394) at android.app.Dialog.show(Dialog.java:295) at android.app.DialogFragment.onStart(DialogFragment.java:499) at android.app.Fragment.performStart(Fragment.java:2244) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) at android.app.BackStackRecord.run(BackStackRecord.java:793) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2
Problem is in "AlertController" Share snippet of Alert controllercom.android.internal.app.AlertController.setupCustomContent(AlertController.java:585) at com.android.internal.app.AlertController.setupView(AlertController.java:496) at com.android.internal.app.AlertController.installContent(AlertController.java:234) atHitesh Sahu
Why you have assigned driverUpdateStatus.view?Pratik Popat
Do you want to add custom view to alert dialog?Pratik Popat
im fetch buttons from dialog_driver_update_status.xmlshashidhar

2 Answers

2
votes
//if (driverUpdateStatus == null) {
        driverUpdateStatus = new DriverUpdateStatus();
        driverUpdateStatus.view = LayoutInflater.from(context).inflate(R.layout.dialog_driver_update_status, null);
//}

Always use new View, Or remove from its parent when you used it.

0
votes

You put the view that existed in layout again at this line .setView(this.view)