指定的子项已有父项。你必须调用removeView()

时间:2017-06-30 11:03:09

标签: android

我有一个类来创建对话框和编码以从中获取值。它适用于一个人。当我第二次尝试调用对话框时,它会传递以下错误消息。

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>
  

处理:com.jackfruitsystems.fleetoperate,PID:4759                                                                                java.lang.IllegalStateException:指定的子节点已经有了   家长。您必须首先在孩子的父母上调用removeView()。                                                                                    在android.view.ViewGroup.addViewInner(ViewGroup.java:4310)                                                                                    在android.view.ViewGroup.addView(ViewGroup.java:4146)                                                                                    在android.view.ViewGroup.addView(ViewGroup.java:4118)                                                                                    在   com.android.internal.app.AlertController.setupCustomContent(AlertController.java:585)                                                                                    在   com.android.internal.app.AlertController.setupView(AlertController.java:496)                                                                                    在   com.android.internal.app.AlertController.installContent(AlertController.java:234)                                                                                    在android.app.AlertDialog.onCreate(AlertDialog.java:423)                                                                                    在android.app.Dialog.dispatchOnCreate(Dialog.java:394)                                                                                    在android.app.Dialog.show(Dialog.java:295)                                                                                    在android.app.DialogFragment.onStart(DialogFragment.java:499)                                                                                    在android.app.Fragment.performStart(Fragment.java:2244)                                                                                    在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1002)                                                                                    在   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)                                                                                    在android.app.BackStackRecord.run(BackStackRecord.java:793)                                                                                    在   android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)                                                                                    在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:482)                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:148)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:2)

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

始终使用新视图,或在使用时从其父视图中删除。

答案 1 :(得分:0)

您将布局中存在的视图再次放在此行.setView(this.view)