I am using android studio and when I click the button the program crashes. The if statements don't seem to be working. Please could you help. import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;
public class customerDetails extends AppCompatActivity {
EditText NameEditText, editTextPhoneNumber, postcodeEditText;
Button Continue;
String Name, postcode, PhoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_customer_details);
Continue = (Button) findViewById(R.id.Continue);
NameEditText = (EditText) findViewById(R.id.NameEditText);
postcodeEditText = (EditText) findViewById(R.id.postcodeEditText);
final String strFromActivity = getIntent().getStringExtra("MY_INFO");
Continue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Name = NameEditText.getText().toString();
postcode = postcodeEditText.getText().toString();
PhoneNumber = editTextPhoneNumber.getText().toString();
if (Name.length() == 0 || !(Name.matches("[a-zA-Z]"))) {
NameEditText.requestFocus();
NameEditText.setError("FIELD CANNOT BE EMPTY AND ONLY HAVE ALPABETICAL CHARACTERS");
}
else if (postcode.length() == 0 || !(postcode.matches("[a-zA-Z1-9]+"))) {
postcodeEditText.requestFocus();
postcodeEditText.setError("FIELD CANNOT BE EMPTY AND ENTER ONLY ALPHABETICAL CHARACTER OR NUMBER");
}
else if (PhoneNumber.length() == 0 || (!(PhoneNumber.matches("[^[0-9]*$]+")))) {
editTextPhoneNumber.requestFocus();
editTextPhoneNumber.setError("FIELD CANNOT BE EMPTY");
}
else {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"woodensigns414@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Order");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Product" + strFromActivity + "Customer Name" + NameEditText);
Toast.makeText(getApplicationContext(), "Order Complete", Toast.LENGTH_SHORT).show();
try {
startActivity(Intent.createChooser(emailIntent, "Send Details..."));
finish();
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(customerDetails.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
Intent intent2 = new Intent(customerDetails.this, OrderComplete.class);
startActivity(intent2);
}
}
});
}
}
Aucun commentaire:
Enregistrer un commentaire