I'm using the LayoutInflater
within a Dialog
and don't know what to set as a 2nd parameter, which is null
for now.
I found answers for onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle)
, but that method isn't available for a Dialog
.
Faking the null
by something like (ViewGroup) null
is not an option for me.
MyDialog
public class MyDialog extends Dialog implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.my_dialog, null);
// ________________ How to replace that null? ___________________^
setContentView(view);
}
}
Error reported by Infer:
MyDialog.java:42: error: ERADICATE_PARAMETER_NOT_NULLABLE
`LayoutInflater.inflate(...)` needs a non-null value in parameter 2 but argument `null` can be null. (Origin: null constant at line 42).
41. LayoutInflater inflater = LayoutInflater.from(getContext());
42. > View view = inflater.inflate(R.layout.dialog_unlock, null);
Any ideas? Thanks in advance!
Solution
public class MyDialog extends Dialog implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_dialog);
Button myBtn = findViewById(R.id.my_btn);
EditText myTextField = findViewById(R.id.my_et);
View.OnClickListener onClickMyBtn = v -> {
String value = myTextField.getText().toString();
Log.d("MyDialog", String.format("My value: %s", value));
dismiss();
};
myBtn.setOnClickListener(onClickMyBtn);
}
}