在另一个活动中显示Double

时间:2016-05-13 17:47:10

标签: java android

我正在尝试在另一个类中显示此类的双..

所以这是我的代码:

公共类计算器扩展了AppCompatActivity {

Button next;

TextView pPrice;
TextView renovations;
TextView misc2;

TextView util;
TextView rep;
TextView mortage;
TextView misc1;

TextView rent;

public double getStartingCostsResult() {
    return startingCostsResult;
}

double startingCostsResult;
double monthlyMinus;
double monthlyPlus;

double monthlyROI;
double yearlyROI;

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

    // Setting these textviews to those in the xml.

    pPrice = (TextView) findViewById(R.id.pPrice);
    renovations = (TextView) findViewById(R.id.renovations);
    misc2 = (TextView) findViewById(R.id.misc2);

    util = (TextView) findViewById(R.id.util);
    rep = (TextView) findViewById(R.id.rep);
    mortage = (TextView) findViewById(R.id.mortage);
    misc1 = (TextView) findViewById(R.id.misc);

    rent = (TextView) findViewById(R.id.rent);


    next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent expense = new Intent(getApplicationContext(), Results.class);


                if ((pPrice.getText().length() > 0) &&  (renovations.getText().length() > 0) && (misc2.getText().length() > 0)) {

                    double price = Double.parseDouble(pPrice.getText().toString());
                   // double costs = Double.parseDouble(cCosts.getText().toString());
                    double reno = Double.parseDouble(renovations.getText().toString());
                    double misc = Double.parseDouble(misc2.getText().toString());


                    startingCostsResult = price + reno + misc;

                    if((util.getText().length()>0) && (rep.getText().length()>0) && (mortage.getText().length()>0) && (misc1.getText().length()>0)){

                        double utilities = Double.parseDouble(util.getText().toString());
                        double repairs = Double.parseDouble(rep.getText().toString());
                        double mort = Double.parseDouble(mortage.getText().toString());
                        double miscsell = Double.parseDouble(misc1.getText().toString());

                        monthlyMinus = utilities + repairs + mort + miscsell;

                       if (rent.getText().length()>0){
                           double monthlyRent = Double.parseDouble(rent.getText().toString());

                           monthlyPlus = monthlyRent;

                           monthlyROI = monthlyPlus - monthlyMinus;
                           yearlyROI = monthlyROI *12;

                           startActivity(expense);
                       }else{
                           Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();


                       }
                    }else{
                        Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
                }


            }

        });









}

}

所以我试图在另一个类中显示annualROI double。

我试过这个:

Calculator calc = new Calculator();
otherClass.setText((int) calc.yearlyROI);

但是当我点击下一步时我的应用程序崩溃了。

2 个答案:

答案 0 :(得分:1)

你应该在费用意图中添加额外费用。

expense.putExtra("yearlyRoi",yearlyRoi);

然后在nexet活动中你就可以得到它。

Intent recievedIntent = this.getIntent();
double yearlyRoi = recievedIntent.getDoubleExtra("yearlyRoi", defaultValue);

默认值可以是0.0或任何你想要的。

至于崩溃,我认为是另一个问题,你需要给我们你的应用程序的错误日志。

答案 1 :(得分:1)

如果您想从其他活动中访问变量,则需要将它们添加到您的意图中。

在你的情况下:

expense.putExtra("yearlyROI", yearlyROI);
startActivity(expense);

然后在你的新活动中:

double yearlyROI = getIntent().getDoubleExtra("yearlyROI");

希望它有所帮助!