带匕首的AlertDialog-您必须先在孩子的父级上调用removeView()-Android

时间:2018-12-04 12:53:13

标签: android android-alertdialog dagger-2

我在AlertDialog中使用Dagger,就像下面这样:

@Module(includes = {
        WorkTableFragmentModule.WorkTableAPIViewAbstractModule.class,
        RemoteAPIDataModule.class
})
public class WorkTableFragmentModule {
  ...Some code...
      @Provides
    final AlertDialog.Builder alertDialogAccess(WorkTableFragment workTableFragment) {
        return new AlertDialog.Builder(Objects.requireNonNull(workTableFragment.getActivity()));
    }
    @Provides
    LayoutInflater layoutInflater(WorkTableFragment workTableFragment) {
        return Objects.requireNonNull(workTableFragment.getActivity()).getLayoutInflater();
    }
    @Provides
    View viewDialogAccess(LayoutInflater layoutInflater) {
        final ViewGroup nullParent = null;
        return layoutInflater.inflate(R.layout.dialog_access_share, nullParent);
    }
}

Fragment中就像下面这样:

public class WorkTableFragment extends DaggerFragment{
@Inject
AlertDialog.Builder alertDialogAccess;
@Inject
View accessAlertDialogView;
@Inject
LayoutInflater inflater;

    private void accessAlertDialog() {
        if (getActivity() != null && isAdded()) {
            alertDialogAccess.setCancelable(false);
            new DialogViewHolderAcceptShare(accessAlertDialogView);
            alertDialogAccess.setView(accessAlertDialogView);
            dismissAccessDialog = alertDialogAccess.show();
        }
    }
}

但是让我听到以下错误:

Caused by: 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:4656)
        at android.view.ViewGroup.addView(ViewGroup.java:4497)
        at android.view.ViewGroup.addView(ViewGroup.java:4469)
        at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:657)
        at android.support.v7.app.AlertController.setupView(AlertController.java:475)
        at android.support.v7.app.AlertController.installContent(AlertController.java:233)
        at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:642)
        at android.app.Dialog.show(Dialog.java:341)
        at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:1007)
        at xx.xx.xx.fragments.worktablefragment.WorkTableFragment.accessAlertDialog(WorkTableFragment.java:343)
        at xx.xx.xx.fragments.worktablefragment.WorkTableFragment.onRequestPermissionsResult(WorkTableFragment.java:409)
        at android.support.v4.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:860)
        at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7434)
        at android.app.Activity.dispatchActivityResult(Activity.java:7260)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4468)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4515) 
        at android.app.ActivityThread.-wrap22(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6682) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

1 个答案:

答案 0 :(得分:1)

解决了我的以下问题:

private void accessAlertDialog() {
    if (getActivity() != null && isAdded()) {
        if (dismissAccessDialog == null) {
            alertDialogAccess.setCancelable(false);
            new DialogViewHolderAcceptShare(accessAlertDialogView);
            alertDialogAccess.setView(accessAlertDialogView);
            dismissAccessDialog = alertDialogAccess.show();
        }else {
            dismissAccessDialog.show();
        }
    }
}