0

因此,我试图在从菜单选项打开的警报对话框中制作联系表格。我的表单和 Main.java 中有 3 个 EditText 字段,当按下表单中的“发送”按钮然后我启动电子邮件意图时,我从这些字段中读取,或者至少这就是它应该做的。现在,只要我按下发送按钮,应用程序就会崩溃。现在我已经解决了这个问题,它似乎不是故意的,但是当我从 EditText 字段中读取时它就会发生。当我取出 EditText 阅读部分并将填充信息放在其位置时,代码工作正常,但我需要它与 EditTexts 一起使用。谢谢你。我在菜单中的选项的整个代码:

case R.id.menu_feedback:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.menu_feedback);
LayoutInflater inflater = this.getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.feedback, null));
// Add the buttons
builder.setNegativeButton(R.string.send, new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           // User clicked OK button
           final EditText nameField = (EditText) findViewById(R.id.EditTextName);
           String name = nameField.getText().toString();
           final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
           String email = emailField.getText().toString();
           final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
           String feedback = feedbackField.getText().toString();
           final CheckBox responseCheckbox = (CheckBox) findViewById(R.id.CheckBoxResponse);
           boolean bRequiresResponse = responseCheckbox.isChecked();

           /* Create the Intent */
           final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

           /* Fill it with Data */
           emailIntent.setType("plain/text");
           emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, R.string.send_email);
           emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
           emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, feedback);

           /* Send it off to the Activity-Chooser */
           startActivity(Intent.createChooser(emailIntent, "Send mail..."));
           dialog.dismiss();

       }
   });
builder.setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               dialog.dismiss();
           }
       });

// create alert dialog
AlertDialog alertDialog = builder.create();

// show it
alertDialog.show();

return true;
4

2 回答 2

2

好的,我假设你所有的 EditTexts 都写在feedback layoutAlertDialog 的文件中,

View dialogView = inflater.inflate(R.layout.feedback, null);
builder.setView(dialogView );

而Edittext就像,

// User clicked OK button
final EditText nameField = (EditText) dialogView.findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
final EditText emailField = (EditText) dialogView.findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
final EditText feedbackField = (EditText) dialogView.findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();

实际上,当您膨胀任何视图并且您必须使用该视图的子视图时,您必须使用该视图和findViewById()方法的引用。所以在你的情况下,dialogView.findViewById();

于 2013-01-28T06:15:59.763 回答
1

使用builderinflater实例从 AlertDialog 访问 EditText 或其他视图:

 builder.setNegativeButton(R.string.send, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
                           // User clicked OK button
   final EditText nameField = (EditText)inflater. findViewById(R.id.EditTextName);
   String name = nameField.getText().toString();
   final EditText emailField = (EditText)inflater. 
                                                 findViewById(R.id.EditTextEmail);
   String email = emailField.getText().toString();
   final EditText feedbackField = (EditText)inflater. 
                                          findViewById(R.id.EditTextFeedbackBody);
   String feedback = feedbackField.getText().toString();
// your code here..
于 2013-01-28T06:14:44.767 回答