如何将mainactivity的整数计算移动到其他活动textview?

时间:2019-05-16 22:30:38

标签: java android

在弄清楚如何通过查看其文本视图将整数计算从主要活动转移到其他活动时遇到问题。

尝试将意图方法放在活动2上,但无法将其链接到Mainactivity.java

MainActivity

else {

                    int P = Integer.parseInt(etPrincipal.getText().toString().trim());
                    int PMT = Integer.parseInt(etRegular.getText().toString().trim());
                    int r = Integer.parseInt(etRate.getText().toString().trim());
                    int n = Integer.parseInt(etTimes.getText().toString().trim());
                    int t = Integer.parseInt(etYears.getText().toString().trim());

                    int calculation = (P *(1 + (r / n) )^(n*t));


                    Intent gotoactivity2 = new Intent( MainActivity.this, com.app.compoundinginterestproject.Activity2.class);
                    gotoactivity2.putExtra("name", calculation);
                    startActivity(gotoactivity2);
                } 

Activity2

  TextView tvone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);

        tvone = findViewById(R.id.tvone);

        Intent calculate = getIntent(Activity2.this, com.app.compoundinginterestproject.MainActivity.class);
        int calculate = MainActivity.getIntExtra("name", 0")

        tvone.setText(calculate + " is the calculation ");

    }
}

我希望对Activity2 Textview输出的输入计算 但出现错误:未封闭的字符串文字

1 个答案:

答案 0 :(得分:0)

您的错误与传递值的Intent逻辑无关。这是发生的编译时错误,因为您在不应该出现的0旁边放了一个双引号

int calculate = MainActivity.getIntExtra("name", 0")

将其删除,因此该行现在如下所示:

int calculate = MainActivity.getIntExtra("name", 0)