簡體   English   中英

如何在Android的同一Activity中從另一個方法調用結果

[英]How to call the result from another method in the same Activity in Android

我正在使用計算器,但遇到了一些問題。 當我嘗試運行該程序時,可以顯示第一個屏幕讓我輸入數字(在這種情況下,我忽略了setText的最后2個代碼作為結果)。 在將數據放入Edittext字段並按下按鈕后,它將被停止(不幸的是,該應用程序已停止)

如下所示的代碼是在填充所有信息后按下按鈕時將調用的方法,並且在另一個方法中分別創建了公式(totalCost和profit變量)。

我不確定這是否是將另一個方法的結果分配給變量的正確方法,也不確定為什么setText中的toString()方法不起作用(錯誤說不能取消引用double)

這是ref的代碼:

public void sendInput(View view) {
    EditText editTextPurPrice = (EditText) findViewById(R.id.editText_PP);
    EditText editTextSelPrice = (EditText) findViewById(R.id.editText_SP);
    EditText editTextStkQty = (EditText) findViewById(R.id.editText_QTY);
    EditText editTextLotSize = (EditText) findViewById(R.id.editText_LS);
    TextView resultTTCost = (TextView) findViewById(R.id.resultTTCost);
    TextView resultProfit = (TextView) findViewById(R.id.resultProfit);

    Double msgPurPrice = Double.parseDouble(editTextPurPrice.getText().toString());
    Double msgSelPrice = Double.parseDouble(editTextSelPrice.getText().toString());
    int msgStkQty = Integer.parseInt(editTextStkQty.getText().toString());
    int msgLotSize = Integer.parseInt(editTextLotSize.getText().toString());

    double totalCost = getTotalCost(msgPurPrice, msgSelPrice, msgStkQty, msgLotSize);
    double profit = getProfit(msgPurPrice, msgSelPrice, msgStkQty, msgLotSize);

    resultTTCost.setText(totalCost.toString());
    resultProfit.setText(profit.toString());
}

希望有人可以幫助我...

問題ia setext它應該如下

    resultTTCost.setText(""+totalCost);
    resultProfit.setText(""+profit);

要么

  resultTTCost.setText(String.valueOf(totalCost));
  resultProfit.setText(String.valueOf
resultTTCost.setText(String.valueOf(totalCost));
resultProfit.setText(String.valueOf(profit));

您需要將double轉換為String。 另一個實現是這樣的:

double aDouble = 0.11;
String aString = Double.toString(aDouble);

暫無
暫無

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

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