Bind ButterKnife to Dialog fails
Asked Answered
V

3

16

I try to bind ButterKnife to a AleterDialog that i made with a DialogBuilder method And exist this method ButterKnife.bind(Object,Dialog); but dosen't work for me

import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class NewUserDialogFragment extends DialogFragment {
    @Bind(R.id.textuserAccount)
    EditText textuserAccount;
    @Bind(R.id.textPassword)
    EditText textPassword;
    @Bind(R.id.nauta_domains)
    Spinner nauta_domains;
    @Bind(R.id.manualConfig)
    View manualConfig;
    @Bind(R.id.checkViewPass)
    CheckBox checkViewPass;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.new_user_dialog__icon)
                .setTitle(R.string.new_user_dialog_title)
                .setView(R.layout.accountlist_dialog_user_)
                .setPositiveButton(R.string.alert_dialog_create, void_OnClickListener)
                .setNegativeButton(R.string.alert_dialog_cancel, void_OnClickListener)
                .create();
      //Fails!!!!!!!
        ButterKnife.bind(this,dialog);  
    ...

ERROR:

Caused by: java.lang.IllegalStateException: Required view 'textuserAccount' with ID 2131624044 for field 'textuserAccount' was not found. If this view is optional add '@Nullable' annotation.
                                                                                      at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)

and textuserAccount = (EditText) ((Dialog) dialog).findViewById(R.id.textuserAccount); works perfectly

I can use butterknife somehow in this class?

Vintager answered 11/5, 2016 at 21:1 Comment(0)
F
56

You need to inflate your dialog layout and pass the resulting View object to butterknife.

    view = View.inflate(getContext(), R.layout.accountlist_dialog_user_, null);
    ButterKnife.bind(this, view);

At least, that's how I've used Butterknife in dialogs and it works fine for me.

Fanchon answered 11/5, 2016 at 21:13 Comment(2)
OK the final solution is pass this inflated view to dialog : new AlertDialog.Builder(getActivity())...setView(view)Vintager
Fixed my problem. Thanks bud.Pakistan
R
5

I was able to bind views in onStart of the DialogFragment (similarly to this sample app), while still using the AlertDialog.Builder#setView(int) method:

private Unbinder unbinder;

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.new_user_dialog__icon)
            .setTitle(R.string.new_user_dialog_title)
            .setView(R.layout.accountlist_dialog_user)
            .setPositiveButton(R.string.alert_dialog_create, void_OnClickListener)
            .setNegativeButton(R.string.alert_dialog_cancel, void_OnClickListener)
            .create();
}

@Override
public void onStart() {
    super.onStart();
    unbinder = ButterKnife.bind(this, getDialog());
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}
Racehorse answered 23/8, 2017 at 10:47 Comment(0)
M
1

And everything works perfect

Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.dialog_outcome); Unbinder unbinder = ButterKnife.bind(this, dialog);

Memling answered 2/3, 2019 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.