Value of passed integer always resets back to default value

前端 未结 1 1107
刺人心
刺人心 2021-01-27 12:46

Bal keeps resetting to 0.

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

Continue.setOnClickListener(new View.OnClickListener() {
    @Override         


        
1条回答
  •  礼貌的吻别
    2021-01-27 13:21

    You're putting 0 in the Intent for "intBalance", instead of putting the value 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 - Try to follow naming conventions, although it might seem inconsequential, it's easy to miss simple things when variable names look like class names.

    Also, don't unnecessarily create variables like startIntent, RecordExpense & RecordExpense. getIntent() already gives you access to the intent, you don't need to get and store a reference to it in a variable everytime.

    0 讨论(0)
提交回复
热议问题