[英]Android App crashes when I try to capture from EditText
So I am trying to make a contact form in an Alert Dialogue box which opens from a menu option. 因此,我正在尝试从菜单选项中打开的“警报对话”框中创建联系表。 I have 3 EditText fields in my form and in my Main.java I read from those fields when the Send button in form is pressed and then I start an Email intent, or at least thats what it is supposed to do.
我的表单中有3个EditText字段,在Main.java中,当按下表单中的“发送”按钮时,我从这些字段中读取内容,然后启动电子邮件意图,或者至少就是该做的事情。 Right now the app crashes as soon as I press the send button.
现在,当我按下发送按钮时,应用程序崩溃了。 Now I have troubleshooted the problem and it doesnt seem to be in the intent but it occurs when I read from the EditText fields.
现在,我已经解决了该问题,似乎不是故意的,但是当我从EditText字段中读取时会发生。 The code works fine when I take out the EditText reading part and just put filler information in its place, but I need this to work with the EditTexts.
当我取出EditText阅读部件并将填充信息放入其位置时,代码可以正常工作,但是我需要使用它来与EditTexts配合使用。 Thank you.
谢谢。 My whole code for the option in the menu:
菜单中选项的全部代码:
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;
Ok, I assume your all EditTexts are written in feedback layout
file for AlertDialog, 好的,我假设您所有的EditText都写在AlertDialog的
feedback layout
文件中,
View dialogView = inflater.inflate(R.layout.feedback, null);
builder.setView(dialogView );
And Edittext are like, 和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();
Actually, when you are inflating any view and you have to use child views of that views then you have to use the reference of that view and findViewById()
method. 实际上,当您放大任何视图并且必须使用该视图的子视图时,则必须使用该视图的引用和
findViewById()
方法。 So in your case, dialogView.findViewById();
因此,在您的情况下,请使用
dialogView.findViewById();
use builder
or inflater
instances for Accessing EditText or other views from AlertDialog as : 使用
builder
或inflater
实例从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..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.