我试图用一个对话框提示用户,该对话框包含两个EditText框,一个正面和负面按钮。当我尝试从EditText框中检索值时,我得到的问题是一个空指针异常。这是我的代码,
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater inflater = LayoutInflater.from(context);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("New Location");
builder.setView(inflater.inflate(R.layout.new_location_dialog, null));
final EditText titleBox = (EditText)findViewById(R.id.title);
final EditText descriptionBox = (EditText)findViewById(R.id.description);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int button) {
String title = titleBox.getText().toString();
String description = descriptionBox.getText().toString();
Log.d("User Setting title / description to: ", title + " : " + description);
//Add new Point to Map
addGeoPointToMap(mapOverlays,longpressLocation,title, description);
return;
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("Cancel setting Title & adding point","");
return;
}
});
builder.show();
如果有人能帮助我,我会很感激!此外,奖励标记,如果有人可以解释在整个应用程序中处理多个对话框的最佳约定是什么。
答案 0 :(得分:7)
您没有正确引用EditText对象。
执行以下操作,它将起作用:
View v = inflater.inflate(R.layout.new_location_dialog, null);
builder.setView(v);
//here is your mistake:
final EditText titleBox = (EditText)v.findViewById(R.id.title);
final EditText descriptionBox = (EditText)v.findViewById(R.id.description);