下面的第一个代码块是我在其中创建第二个活动意图的主要活动。在这个活动中,我在列表视图中显示费用,现在我已经遗漏了,因为它没有完全实现。我想做的就是启动第二个活动,让用户输入详细信息,然后按一个按钮将活动添加到列表视图中。 @覆盖 public boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId();
if (id == R.id.addExpense) {
Intent intent = new Intent(this, ExpenseActivity.class);
startActivityForResult(intent, 1);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check that it is the SecondActivity with an OK result
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Expense expense = (Expense) data.getSerializableExtra("sampleObject");
Expenses.add(expense);
}
}
}
final Button btnAddExpense = (Button) findViewById(R.id.btnAddExpense);
btnAddExpense.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String amountV = txtAmountVat.getText().toString();
int amountVTwo = Integer.parseInt(amountV);
String amountI = txtAmount.getText().toString();
int amountITwo = Integer.parseInt(amountI);
Expense expense = new Expense(amountITwo, amountVTwo, txtDateOfExpense.getText().toString(), txtDateAdded.getText().toString(), datePaid, paid, txtDes.getText().toString(), imageUri );
Intent intent = new Intent();
intent.putExtra("Expense", expense);
setResult(MainActivity.RESULT_OK, intent);
finish();
}
});
这是我用户输入数据的第二个活动。当我尝试传回费用对象时,模拟器声明应用程序已停止工作。我可以帮忙,因为我不知道是什么导致了这个问题。这就是我班上的样子。
public class Expense implements Serializable {
private int _amount, _amountVat;
private String _dateOfExpense, _dateAdded, _datePaid, _expenseDescription;
private Boolean _paid;
private Uri _imageUri;
public Expense(int amount, int amountVat, String dateOfExpense, String dateAdded, String datePaid, Boolean paid, String expenseDescription, Uri imageUri){
_amount = amount;
_amountVat = amountVat;
_dateOfExpense = dateOfExpense;
_dateAdded = dateAdded;
_datePaid = datePaid;
_paid = paid;
_expenseDescription = expenseDescription;
_imageUri = imageUri;
}
public int get_amount() {
return _amount;
}
public void set_amount(int _amount) {
this._amount = _amount;
}
public int get_amountVat() {
return _amountVat;
}
public void set_amountVat(int _amountVat) {
this._amountVat = _amountVat;
}
public String get_dateOfExpense() {
return _dateOfExpense;
}
public void set_dateOfExpense(String _dateOfExpense) {
this._dateOfExpense = _dateOfExpense;
}
public String get_dateAdded() {
return _dateAdded;
}
public void set_dateAdded(String _dateAdded) {
this._dateAdded = _dateAdded;
}
public String get_datePaid() {
return _datePaid;
}
public void set_datePaid(String _datePaid) {
this._datePaid = _datePaid;
}
public Boolean get_paid() {
return _paid;
}
public void set_paid(Boolean _paid) {
this._paid = _paid;
}
public Uri get_imageUri() {
return _imageUri;
}
public void set_imageUri(Uri _imageUri) {
this._imageUri = _imageUri;
}
public String get_expenseDescription() {return _expenseDescription;}
public void set_expenseDescription(String _expenseDescription) {this._expenseDescription = _expenseDescription;}
}
答案 0 :(得分:0)
如果没有正确的日志详细信息,很难说出你的问题。 但你可以通过这些观点。 Serializable方法的问题在于使用了反射并且这是一个缓慢的过程。此方法会创建大量临时对象并导致相当多的垃圾回收。所以,可能是由于这个原因。尝试在真实设备上运行&看它是否仍然存在。 或者,您可以将Parcelable实现为比Serializable更快的类。