【发布时间】:2019-06-23 13:24:55
【问题描述】:
我在Dialog 中使用LayoutInflater,但不知道将什么设置为2nd 参数,目前为null。
我为onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle bundle) 找到了answers,但该方法不适用于Dialog。
伪造null 之类的(ViewGroup) null 对我来说不是一种选择。
我的对话框
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);
}
}
错误由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);
有什么想法吗?提前致谢!
解决方案
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);
}
}
【问题讨论】:
标签: java android android-layout null