简体   繁体   English

使用Android中的可序列化将对象从第二个活动传递回主活动

[英]Passing an object from the 2nd activity back to main activity using serializable in android

The first block of code below is my main activity in which I created the intent to the second activity. 下面的第一段代码是我的主要活动,在其中创建了第二个活动的意图。 On this activity I am displaying the expense in a list view which for now I have left out as it is not fully implemented. 在此活动中,我将在列表视图中显示费用,但由于未完全实现,因此我暂时将其忽略了。 What I simple want to do is launch the second activity and let the user enter in details and press a button to add the activity to the list view. 我简单想做的是启动第二个活动,然后让用户输入详细信息,然后按一个按钮以将该活动添加到列表视图中。 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); @Override 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();
        }
        });

And this is my second activity in which the user enters in data. 这是我的第二项活动,用户在其中输入数据。 When i try pass back the expense object the emulator states the app has stopped working. 当我尝试传递费用对象时,仿真器指出应用已停止工作。 Please could I have some help as I don't know what is causing this problem. 由于我不知道是什么原因引起的,请给我一些帮助。 This is what my class looks like. 这是我班的样子。

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;}

} }

Much can't be said about your problem without proper log details. 如果没有适当的日志详细信息,就无法说出很多有关您的问题的信息。 But you can go through these points. 但是您可以通过这些步骤。 The problem with Serializable approach is that reflection is used and it is a slow process. Serializable方法的问题在于使用了反射,这是一个缓慢的过程。 This method create a lot of temporary objects and cause quite a bit of garbage collection. 这种方法会创建许多临时对象,并导致大量垃圾回收。 So, it might be due to this. 因此,可能是由于此。 Try running on a real device & see if it persists. 尝试在真实设备上运行并查看其是否仍然存在。 Alternatively, you can implement Parcelable to your class which is faster than Serializable. 另外,您可以将Parcelable实现到您的类,这比Serializable更快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递ArrayList <Object> 返回主要活动(Android) - Passing ArrayList<Object> back to main Activity (Android) 在第二活动中更改主变量 - Chaning a Variable in Main from 2nd activity Android:将可序列化的对象传递给Activity时发生ClassNotFoundException - Android: ClassNotFoundException when passing serializable object to Activity 将可序列化的对象传递给另一个活动 - Passing serializable object to another activity Android:从活动到活动,第二个活动无法调用setContentView? - Android: Activity to Activity, 2nd activity cannot call setContentView? 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 当我删除 <action android:name=“android.intent.action.MAIN” /> 从清单中,应用程序从第二个活动开始 - When I remove the <action android:name=“android.intent.action.MAIN” /> from the manifest the the application starts from the 2nd activity 将对象从活动传递到活动android eclipse - passing object from activity to activity android eclipse 使用android中的putextra和getextra方法从第二个活动转到第一个活动时图像视图的可见性消失了 - visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM