gpt4 book ai didi

java - 在 AlertDialog 中检索用户信息

转载 作者:行者123 更新时间:2023-12-01 14:52:56 26 4
gpt4 key购买 nike

我一直在努力简化和简化,以从 AlertDialog 中的 EditText 获取文本字符串,但没有成功

private void showAddSectorDlg() {
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_ACTSECTORS, "showAddSectorDialog() called.");
}
LayoutInflater inflater = getLayoutInflater();
new AlertDialog.Builder(ActSectors.this)
.setTitle(R.string.menu_new_sector)
.setMessage(R.string.dlg_sect_add_msg)
.setView(inflater.inflate(R.layout.dlg_sector_add, null))
.setPositiveButton(R.string.dlg_save,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int whichButton) {
EditText mNewSectorName = (EditText) findViewById(R.id.dlg_edt_nsector);
//NullPointerException happens here:
String val = mNewSectorName.getText().toString();
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_ACTSECTORS, "New Sector Name: "+val);
}
grabVal(val);
dialog.dismiss();
}
})
.setNegativeButton(R.string.dlg_cancel,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int whichButton) {
}
}).create().show();
}

private void grabVal(String newSector){
mNSN = newSector;
callCtlr(Constants.R_DB_ADDSECT);
}

如果有人能解释我的错误在哪里,我将不胜感激......谢谢!

最佳答案

更改代码以从 AlertDialog 的 EditText 获取值:

 LayoutInflater inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.dlg_sector_add,null);
new AlertDialog.Builder(ActSectors.this)
.setTitle(R.string.menu_new_sector)
.setMessage(R.string.dlg_sect_add_msg)
.setView(view)
//... your code here

现在使用 View 从 AlertDialog 访问 EditText :

EditText mNewSectorName = (EditText)view.findViewById(R.id.dlg_edt_nsector);

关于java - 在 AlertDialog 中检索用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14663019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com