【问题标题】:How to call removeView() on dialog box number one so as to show dialog number 2如何在第一个对话框上调用 removeView() 以显示第 2 个对话框
【发布时间】:2018-05-24 15:15:35
【问题描述】:

我正在使用一个应用程序,该应用程序涉及应该一个接一个出现的对话框。我的意思是,当我单击第一个对话框的肯定按钮时,它应该会弹出另一个第二个对话框。我面临的问题是第二个对话框,第二个不会弹出,它会导致应用程序崩溃。

我收到一条错误消息,提示“java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。”

public static void showBusinessOrPrivateStartDialog(final Context context, final CallLogEntry call)
       {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
          {
             // If new version of android, then check permission
             if (!Settings.canDrawOverlays(context))
             {
                Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
                return;
             }
          }

          LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          //View view = inflater.inflate(R.layout.dialog_call_type, null);

          AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);

          builder.setTitle("Was this a private or a business call?");
          builder.setMessage("Please select a call type");
          builder.setIcon(R.drawable.ic_deviceinsight);
          builder.setCancelable(true);
          //builder.setView(view);

          builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
             @Override
             public void onCancel(DialogInterface dialog) {
                // Occurs when user cancels the dialog, or clicks somewhere else in the screen or presses the back button
                Log.i(TAG, "cancelled");

                saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", true, "");
             }
          });

          builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                CallClassification.showBusinessCallPopup(context, call);
             }
          });
          builder.setNegativeButton("Private", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                saveCallEntry(context, call, CLASSIFICATION_TYPE_PRIVATE, "", false, "");
             }
          });

          AlertDialog dialog = builder.create();
          // Rather use something like TYPE_SYSTEM_ALERT, if possible.
          dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
          dialog.show();
       }

这是第二个对话框的第二种方法,我的应用程序在 dialog.show 上崩溃

public static void showBusinessCallPopup(final Context context, final CallLogEntry call)
       {
          // NEW NRF
          if (AppPreferences.isNRF(context)) {
             CallItem callItem = saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, "", false, "");

             // SHOW NEW FORM
             Intent intent = new Intent(context, CallClassificationFormActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             EventBus.getDefault().postSticky(callItem);
             context.startActivity(intent);

             return;
          }

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
          {
             // If new version of android, then check permission
             if (!Settings.canDrawOverlays(context))
             {
                Log.e(TAG, "Permission Denied: Draw overlays (for popup)");
                return;
             }
          }

          LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View view = inflater.inflate(R.layout.dialog_call_details, null);

          AlertDialog.Builder builder = new AlertDialog.Builder(context, Theme_Material_Light_Dialog_Alert);

          String toOrFrom = call.Type == CallLog.Calls.INCOMING_TYPE ? "from" : "to";

          //builder.setTitle("Business call");
          builder.setCustomTitle(view);
          //builder.setMessage("Please enter reference details for the call " + toOrFrom + " " + call.Number);
          //builder.setIcon(R.drawable.ic_deviceinsight);
          builder.setCancelable(true);
          builder.setView(view);

          final EditText etReference = (EditText)view.findViewById(R.id.etReference);
          final EditText etComment = (EditText)view.findViewById(R.id.etComment);

          builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {

                String reference = etReference.getText().toString();
                String comment = etComment.getText().toString();

                saveCallEntry(context, call, CLASSIFICATION_TYPE_BUSINESS, reference, false, comment);
             }
          });
          builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
             @Override
             public void onCancel(DialogInterface dialog) {
                Log.i(TAG, "cancelled");
             }
          });

          AlertDialog dialog = builder.create();
          dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); // TYPE_SYSTEM_ERROR
          dialog.show();
       }

这是我的 xml。我在这篇文章中包含了 xml,因为我在某处读到我的文本框的高度和宽度的定义方式 可能会导致问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:paddingLeft="5dp"
              android:paddingRight="5dp"
              android:paddingTop="5dp"
              tools:context=".calls.CallClassification">

  <TextView
         android:id="@+id/tvMessage"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginBottom="5dp"
         android:padding="5dp"
         android:text="Please enter reference details for the call "
         android:textColor="@color/colorPrimaryDark"
         android:textSize="16sp"/>

   <AutoCompleteTextView
         android:id="@+id/etReference"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:backgroundTint="@color/colorAccent"
         android:hint="Matter Code *"
         android:imeOptions="actionNext"
         android:inputType="text"
         android:maxLength="255"
         android:text=""
         android:textColor="@color/colorPrimary"
         android:textColorHint="@color/colorTextHint"
         android:textSize="14sp"/>

   <CheckBox
         android:id="@+id/cbBillable"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="5dp"
         android:layout_marginRight="5dp"
         android:button="@null"
         android:buttonTint="@color/colorAccent"
         android:checked="true"
         android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
         android:paddingBottom="3dp"
         android:paddingTop="3dp"
         android:text="Billable"
         android:textColor="@color/colorPrimaryDark"
         android:textSize="14sp"/>

   <EditText
         android:id="@+id/etComment"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:backgroundTint="@color/colorAccent"
         android:hint="Enter comment"
         android:imeOptions="actionDone"
         android:inputType="text"
         android:lines="1"
         android:maxLength="1000"
         android:maxLines="4"
         android:minLines="1"
         android:text=""
         android:textColor="@color/colorPrimary"
         android:textColorHint="@color/colorTextHint"
         android:textSize="14sp"/>

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

      <Button
            android:id="@+id/bCancel"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"/>

      <Button
            android:id="@+id/bOk"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="GO"/>
   </LinearLayout>


</LinearLayout>

【问题讨论】:

    标签: java android xml modal-dialog illegalstateexception


    【解决方案1】:

    听了您的经验后,我可以想到另一种解决方案。与其在每个方法中声明 AlertDialog 变量,不如声明一个全局变量,这样您就可以确保在任何实例中只显示一个 Dialog。

    在声明时,使

    AlertDialog 对话框 = null;

    在课堂上 然后在,

        showBusinessOrPrivateStartDialog() {
        if(dialog == null) {
        dialog = build.create();
        }
        }
    
    
        builder.setPositiveButton("Business", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        CallClassification.showBusinessCallPopup(context, call);
                     }
                  });
    
    builder.setOnDismissListener(new OnDismissListener() {
    public void onDismiss() {
    dialog = null;
    }
    }
    

    然后再次在 showBusinessCallPopup() 中,

    if(dialog == null) {
        dialog = build.create();
        }
    

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      调用前

      CallClassification.showBusinessCallPopup(context, call);

      你需要打电话

      dialog.dismiss();

      在同一个 onClick() 中。

      希望这能解决您的问题。

      【讨论】:

      • 谢谢,我会尝试然后恢复
      • 不幸的是,即使在插入 dialog.dismiss() 后应用程序仍然崩溃
      【解决方案3】:

      在您的第二个代码中,您添加了两次视图,也许您应该删除其中一个。

          builder.setCustomTitle(view); -> Here
          builder.setCancelable(true);
          builder.setView(view); --> Here
      

      :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-26
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多