找到你要的答案

Q:How to call the result from another method in the same Activity in Android

Q:如何在Android中的同一活动中调用另一方法的结果

I am working on a calculator, and I faced some problems with it. When I try to run the program, it is okay to show the first screen to let me enter the number (in the situation that I ignored the last 2 code of setText for result). After I put the data in the Edittext fields and press the button, it is going to be stopped (Unfortunately the application has been stopped)

The code shown below is the method that will be called when I press the button after all information is filled, and I have the formula created in another method separately (the totalCost and profit variable).

I am not sure if it is the right way to assign the result from another method to the variable, and not sure why the toString() method in setText doesn't work (error saying that double cannot be dereferenced)

Here is the code for 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());
}

Hope someone could help me out...

I am working on a calculator, and I faced some problems with it. When I try to run the program, it is okay to show the first screen to let me enter the number (in the situation that I ignored the last 2 code of setText for result). After I put the data in the Edittext fields and press the button, it is going to be stopped (Unfortunately the application has been stopped)

下面的代码将被调用时,我按下按钮后,所有的信息填充的方法,和我分开的另一种方法创建的公式(总成本和利润的变量)。

我不知道这是分配的结果从另一个方法的变量的正确方式,不知道为什么在不工作的tostring() setText方法(错误称双不能引用)

这里是参考代码:

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());
}

希望有人能帮我…

answer1: 回答1:
resultTTCost.setText(String.valueOf(totalCost));
resultProfit.setText(String.valueOf(profit));

You need to convert double to String. The other implemention is like this:

double aDouble = 0.11;
String aString = Double.toString(aDouble);
resultTTCost.setText(String.valueOf(totalCost));
resultProfit.setText(String.valueOf(profit));

您需要将双字符串转换为。其他的实现是这样的:

double aDouble = 0.11;
String aString = Double.toString(aDouble);
answer2: 回答2:

Problem ia setext it should as below

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

Or

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

问题是Setext应该如下

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

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