我正在试图在Android中创建并将客户对象保存到SQLite,
这是我的客户类
public class Customer {
private int id;
private String Name;
private String EmailAddress;
private String Phone;
private Double InitialValue;
private Double BalanceOnCard = 0.00;
private String CardUID;
private Date EnrollmentDate = new Date();
private Date LastTransactionDate = new Date();
public Customer(){}
在我的Activity中,我创建了像这样的私有变量
public class EnrollmentActivity extends MenuOnlyActivity {
private CustomerSQLiteHelper db;
private final String TAG = "Customer_Add";
Button submitButton;
Button cancelButton;
private EditText customerNameEditText;
private EditText customerEmailEditText;
private EditText customerPhoneEditText;
private EditText valueToAddEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_enrollment);
submitButton = (Button) findViewById(R.id.buttonEnrollment);
cancelButton = (Button) findViewById(R.id.CancelEnrollment);
customerNameEditText = (EditText) findViewById(R.id.editTextCustomerName);
customerEmailEditText = (EditText) findViewById(R.id.editTextCustomerEmailAddress);
customerPhoneEditText = (EditText) findViewById(R.id.editTextBusinessPhoneNumber);
valueToAddEditText = (EditText) findViewById(R.id.editTextValueToAdd);
这里是SubmitButton监听器,它在这个代码块中失败,它没有达到调用DatabaseHelper来保存Customer对象的地步,它在创建一个空白的Customer对象并开始设置后立即失败了实例变量。
Log.i(TAG, "Entering Submit Button event");
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (customerNameEditText.getText().length() == 0) {
Toast.makeText(EnrollmentActivity.this, "Enter Customer's name", Toast.LENGTH_SHORT).show();
customerNameEditText.requestFocus();
return;
}
Log.i(TAG, "Create new customer");
//Create new customer object
Customer enteredCustomer = new Customer();
Log.i(TAG, "Created blank customer object");
Log.i(TAG, "populate the new customer object");
//Populate the values of the new object with the values entered in the screen
enteredCustomer.setId(1);
enteredCustomer.setName(customerNameEditText.getText().toString());
enteredCustomer.setEmailAddress(customerEmailEditText.getText().toString());
enteredCustomer.setPhone(customerPhoneEditText.getText().toString());
String enteredValueSting = valueToAddEditText.getText().toString();
double storedValue = Double.parseDouble(enteredValueSting); //convert String to Double
enteredCustomer.setInitialValue(storedValue);
enteredCustomer.setCardUID("ABC123");
Log.i(TAG, "Creating DB Instance");
db = new CustomerSQLiteHelper(EnrollmentActivity.this);
Log.i(TAG, "Created Database instance");
if (db.create(enteredCustomer) != -1) {
Toast.makeText(EnrollmentActivity.this,"Add Customer Successful",Toast.LENGTH_SHORT).show();
customerNameEditText.getText().clear();
customerEmailEditText.getText().clear();
customerPhoneEditText.getText().clear();
valueToAddEditText.getText().clear();
} else {
Toast.makeText(EnrollmentActivity.this, "Add Customer failed", Toast.LENGTH_SHORT).show();
}
}
});
错误消息到处都是,它始终提供祝酒词#34;不幸的是PROJECT_NAME停止了#34;当前控制台错误是"已建立的连接已被主机中的软件中止 java.io.IOException:已建立的连接被主机中的软件中止了#34;我已经重启了Eclipse和Emulator几次。任何帮助/建议将不胜感激。
答案 0 :(得分:0)
经过多次拔毛之后,我做了两件事来隔离并解决问题,首先我创建了一个默认构造函数并将参数初始化为合理的默认值,对于字符串为null,对于Ints为0,然后我重命名了所有的XML ID以匹配我在Activity中创建的Java变量。现在,我可以创建Customer对象的实例并持久保存到数据库。