Ad

AlertDialog With Dagger - You Must Call RemoveView() On The Child's Parent First - Android

- 1 answer

I am using from AlertDialog with Dagger like bellow:

@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);
    }
}

And in Fragment is like bellow:

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();
        }
    }
}

But get me bellow error:

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) 
Ad

Answer

Resolved my problem like bellow:

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();
        }
    }
}
Ad
source: stackoverflow.com
Ad