本文介绍了将ButterKnife绑定到对话框失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将ButterKnife绑定到我使用DialogBuilder方法创建的AleterDialog上并且存在此方法 ButterKnife.bind(Object,Dialog);
,但对我不起作用
I try to bind ButterKnife to a AleterDialog that i made with a DialogBuilder methodAnd 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);
...
错误:
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)((对话框)对话框).findViewById(R.id.textuserAccount);
效果很好
我可以在这堂课中使用黄油刀吗?
I can use butterknife somehow in this class?
推荐答案
您需要膨胀对话框的布局并将生成的View对象传递给butterknife.
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);
至少,这就是我在对话框中使用Butterknife的方式,对我来说很好用.
At least, that's how I've used Butterknife in dialogs and it works fine for me.
这篇关于将ButterKnife绑定到对话框失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!