簡體   English   中英

在另一個活動中顯示Double

[英]Displaying Double in another acitivity

我正在嘗試在另一個班級顯示一個班級的兩倍。

所以這是我的代碼:

公共類計算器擴展了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翻倍。

我已經試過了:

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

但是,當我單擊下一步時,我的應用程序崩潰了。

您應該在這樣的支出意向中多加一些。

expense.putExtra("yearlyRoi",yearlyRoi);

然后在nexet活動中,您可以像這樣獲得它。

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

默認值可以是0.0或任何您想要的值。

至於崩潰,我認為這是另一個問題,您需要向我們提供您應用的錯誤日志。

如果要訪問其他“活動”中的變量,則需要將其添加到意圖中。

在您的情況下:

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

然后在您的新活動中:

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

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM