传递的整数值始终会重置为默认值

时间:2019-07-17 12:19:28

标签: java android

C: && cd Users\user12\Desktop\Postman newman run collection3.json 一直重置为0。

Bal

另一项活动

Continue = (Button)findViewById(R.id.btnContinue);

Continue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent startIntent = new Intent(SecondActivity.this, Mainmenu.class);
        int Bal = Integer.parseInt(Balance.getText().toString());
        int MonthlyTarget = Integer.parseInt(Target.getText().toString());
        startIntent.putExtra("intBalance", Bal);
        startIntent.putExtra("intTarget", MonthlyTarget);
        startActivity(startIntent);
    }
});

每当我将Intent startIntent = getIntent(); int Bal = startIntent.getIntExtra("intBalance", 0); int MonthlyTarget = startIntent.getIntExtra("intTarget", 0); Intent RecordExpense = getIntent(); int Expense = RecordExpense.getIntExtra("intExpense", 0); Intent RecordIncome = getIntent(); int Income = RecordIncome.getIntExtra("intIncome", 0); TextView tvResult=(TextView) findViewById(R.id.tvResult); tvResult.setText(Bal +""); if(getIntent().hasExtra("intIncome")) { Bal=Bal+Income; tvResult.setText(Bal + ""); } if(getIntent().hasExtra("intExpense")) { Bal=Bal-Expense; tvResult.setText(Bal + ""); } 用作余额,将5用作费用时,我期望输出为3。但是实际输出是2

1 个答案:

答案 0 :(得分:0)

您要在“ Intent”中将“ intBalance”放入0,而不是将值“ Bal”放入

Continue = (Button)findViewById(R.id.btnContinue);

Continue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent startIntent = new Intent(SecondActivity.this, Mainmenu.class);
        int Bal = Integer.parseInt(Balance.getText().toString());
        int MonthlyTarget = Integer.parseInt(Target.getText().toString());
        // Change 0 to Bal
        startIntent.putExtra("intBalance", Bal);
        startIntent.putExtra("intTarget", MonthlyTarget);
        startActivity(startIntent);
    }
});

P-S- 尝试遵循命名约定,尽管看起来似乎无关紧要,但是当变量名看起来像类名时,很容易错过简单的事情。

此外,不要不必要地创建变量startIntentRecordExpenseRecordExpensegetIntent()已经使您可以访问该意图,您无需每次都获取并存储对它的引用。