MyDialogpublic class MyDialog extends Dialog 实现 View.OnClickListener {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LayoutInflater inflater = LayoutInflater.from(getContext());查看视图 = inflater.inflate(R.layout.my_dialog, null);//________________ 如何替换那个空值?_____^设置内容视图(视图);}}错误由Infer报告:MyDialog.java:42: 错误:ERADICATE_PARAMETER_NOT_NULLABLE`LayoutInflater.inflate(...)` 需要参数 2 中的非空值,但参数 `null` 可以为空.(原点:第 42 行的空常量).41. LayoutInflater inflater = LayoutInflater.from(getContext());42. >查看视图 = inflater.inflate(R.layout.dialog_unlock, null);有什么想法吗?提前致谢!解决方案public class MyDialog extends Dialog 实现 View.OnClickListener {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_dialog);按钮 myBtn = findViewById(R.id.my_btn);EditText myTextField = findViewById(R.id.my_et);View.OnClickListener onClickMyBtn = v ->{字符串值 = myTextField.getText().toString();Log.d("MyDialog", String.format("My value: %s", value));解雇();};myBtn.setOnClickListener(onClickMyBtn);}} 解决方案 使用这个setContentView(R.layout.my_dialog);代替这个LayoutInflater inflater = LayoutInflater.from(getContext());查看视图 = inflater.inflate(R.layout.my_dialog, null);设置内容视图(视图);示例代码import android.app.Dialog;导入 android.content.Context;导入 android.os.Bundle;导入 android.view.View;导入 android.widget.Button;导入 android.widget.Toast;公共类 MyDialog 扩展 Dialog 实现 View.OnClickListener {公共 MyDialog(上下文上下文){超级(上下文);}按钮 myBtn;@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_dialog);myBtn = findViewById(R.id.my_btn);myBtn.setOnClickListener(this);}@覆盖公共无效onClick(查看视图){如果(视图== myBtn){Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show();}}}然后像这样创建对话框MyDialog myDialog = new MyDialog(this);myDialog.show();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.MyDialogpublic 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!Solutionpublic 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); }} 解决方案 Use thissetContentView(R.layout.my_dialog);Instead of thisLayoutInflater inflater = LayoutInflater.from(getContext());View view = inflater.inflate(R.layout.my_dialog, null);setContentView(view);SAMPLE CODEimport android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MyDialog extends Dialog implements View.OnClickListener { public MyDialog(Context context) { super(context); } Button myBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_dialog); myBtn = findViewById(R.id.my_btn); myBtn.setOnClickListener(this); } @Override public void onClick(View view) { if (view == myBtn) { Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT).show(); } }}Then create your dialog like thisMyDialog myDialog = new MyDialog(this);myDialog.show(); 这篇关于Android (Java):如何将 LayoutInflater.inflate() 用于对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-12 15:08