gpt4 book ai didi

java - 在另一个 Activity 中显示 Double

转载 作者:行者123 更新时间:2023-12-02 03:37:56 24 4
gpt4 key购买 nike

我正在尝试在另一个类中显示该类的 double 值..

这是我的代码:

公共(public)类计算器扩展了 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();
}


}

});









}

}

所以我试图在另一个类中显示年投资返回率的两倍。

我已经尝试过这个:

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

但是当我单击“下一步”时,我的应用程序崩溃了。

最佳答案

您应该像这样在费用 Intent 中添加额外内容。

expense.putExtra("yearlyRoi",yearlyRoi);

然后在下一个 Activity 中你可以像这样得到它。

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

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

至于崩溃,我认为这是另一个问题,您需要向我们提供您的应用程序的错误日志。

关于java - 在另一个 Activity 中显示 Double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37216323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com