当我尝试从EditText捕获时,Android应用程序崩溃

时间:2013-01-28 06:08:33

标签: android android-intent menu crash

因此,我尝试在“警报对话”框中创建一个联系表单,该框从菜单选项打开。我的表单中有3个EditText字段,在我的Main.java中,当按下表单中的“发送”按钮时,我从那些字段中读取,然后我启动了电子邮件意图,或者至少那是它应该做的事情。现在,只要按下发送按钮,应用就会崩溃。现在我已经解决了这个问题并且它似乎没有出现在意图中,但是当我从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;

2 个答案:

答案 0 :(得分:2)

好的,我假设您的所有EditTexts都写在AlertDialog的feedback layout文件中,

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();

答案 1 :(得分: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..