我尝试将ButterKnife绑定到使用DialogBuilder方法创建的AlertDialog上,存在这个方法 ButterKnife.bind(Object,Dialog);
但对我不起作用。
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);
...
错误:
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)
textuserAccount = (EditText) ((Dialog) dialog).findViewById(R.id.textuserAccount);
这句话可以完美地运行。
我能在这个类里以某种方式使用butterknife吗?
new AlertDialog.Builder(getActivity())...setView(view)
。 - Rolando Corratge Nieves