我需要在片段类中使用getLayoutInflater但是有如下错误:
Error:(290, 17) error: method getLayoutInflater in class Fragment cannot be applied to given types;
required: Bundle
found: no arguments
reason: actual and formal argument lists differ in length
这是我的代码:
private void initDialog(){
alertDialog = new AlertDialog.Builder(getContext());
view = getLayoutInflater().inflate(R.layout.dialog_layout,null);
alertDialog.setView(view);
alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(add){
add =false;
mainListAdapter.addItem(et_country.getText().toString());
dialog.dismiss();
} else {
mainListAdapter.mList.set(edit_position,et_country.getText().toString());
mainListAdapter.notifyDataSetChanged();
dialog.dismiss();
}
}
});
et_country = (EditText)view.findViewById(R.id.et_country);
}
答案 0 :(得分:2)
只需使用:
LayoutInflater inflater = LayoutInflater.from(getActivity());
或
getActivity().getLayoutInflater().inflate(R.layout.dialog_layout,null);
答案 1 :(得分:0)
当您将片段作为参考时使用getActivity()
也会将getActivity()
传递给Builder(getActivity());
alertDialog = new AlertDialog.Builder(getActivity());
view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout,null);