decimalFormat.format()错误

时间:2017-10-01 12:15:10

标签: android kotlin

decimalFormat声明如下:

private var decimalFormat: DecimalFormat? = null 
decimalFormat = DecimalFormat("#.##########")  

要传递给format()方法的值是:

private var valueOne= java.lang.Double.NaN
private var valueTwo: Double = 0.toDouble()

我正在尝试创建一个计算器应用程序,当我将 valueOne 传递给 format()方法时,我得到的错误在操作员按钮中:

 binding!!.buttonAdd.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = ADDITION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "+")
        binding!!.editText.setText(null)
    }

    binding!!.buttonSubtract.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = SUBTRACTION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "-")
        binding!!.editText.setText(null)
    }

    binding!!.buttonMultiply.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = MULTIPLICATION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "*")
        binding!!.editText.setText(null)
    }

    binding!!.buttonDivide.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = DIVISION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "/")
        binding!!.editText.setText(null)
    }

    binding!!.buttonEqual.setOnClickListener {
        computeCalculation()
        binding!!.infoTextView.text = binding!!.infoTextView.text.toString() +
                decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne)
        valueOne = java.lang.Double.NaN
        CURRENT_ACTION = '0'
    }  

我得到的错误是:

None of the following functions can be called with the arguments supplied: 
public final fun format(obj: Any!): String! defined in 
java.text.DecimalFormat
public final fun format(number: Double): String! defined in 
java.text.DecimalFormat
public final fun format(number: Long): String! defined in 
java.text.DecimalFormat   

onCreate()方法的完整代码如下:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

    decimalFormat = DecimalFormat("#.##########")

    binding?.buttonDot?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + ".") }

    binding?.buttonZero?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "0") }

    binding?.buttonOne?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "1") }

    binding?.buttonTwo?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "2") }

    binding?.buttonThree?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "3") }

    binding?.buttonFour?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "4") }

    binding?.buttonFive?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "5") }

    binding?.buttonSix?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "6") }

    binding?.buttonSeven?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "7") }

    binding?.buttonEight?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "8") }

    binding?.buttonNine?.setOnClickListener { binding?.editText?.setText((binding as ActivityMainBinding).editText.text.toString() + "9") }

    binding!!.buttonAdd.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = ADDITION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "+")
        binding!!.editText.setText(null)
    }

    binding!!.buttonSubtract.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = SUBTRACTION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "-")
        binding!!.editText.setText(null)
    }

    binding!!.buttonMultiply.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = MULTIPLICATION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "*")
        binding!!.editText.setText(null)
    }

    binding!!.buttonDivide.setOnClickListener {
        computeCalculation()
        CURRENT_ACTION = DIVISION
        binding!!.infoTextView.setText(decimalFormat.format(valueOne) + "/")
        binding!!.editText.setText(null)
    }

    binding!!.buttonEqual.setOnClickListener {
        computeCalculation()
        binding!!.infoTextView.text = binding!!.infoTextView.text.toString() +
                decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne)
        valueOne = java.lang.Double.NaN
        CURRENT_ACTION = '0'
    }

    binding!!.buttonClear.setOnClickListener {
        if (binding!!.editText.text.length > 0) {
            val currentText = binding!!.editText.text
            binding!!.editText.setText(currentText.subSequence(0, currentText.length - 1))
        } else {
            valueOne = java.lang.Double.NaN
            valueTwo = java.lang.Double.NaN
            binding!!.editText.setText("")
            binding!!.infoTextView.text = ""
        }
    }

}  

请帮我解决错误。

1 个答案:

答案 0 :(得分:1)

请在此处查看DecimalFormat doc DecimalFormat doc

您对此类的使用是错误的,并且错误消息是明确的。 未定义方法格式(obj:Any!)格式(数字:双精度)格式(数字:长)。

你应该使用格式(双号,StringBuffer结果,FieldPosition fieldPosition)