Search code examples
buttonoperatorscharsequence

operator + cannot be applied to java.lang.charsequence


Trying to make a calculator, here's the button method when the line with the * is ran it comes up with the error "operator + cannot be applied to java.lang.charsequence". If any one know's a way around this please help.

    public void Button_Click(View view) {
    TextView result = (TextView) findViewById(R.id.result);
    Button b = (Button)view;
    if ((result.getText() == "0")||(operation_pressed))
        result.setText("");
    operation_pressed = false;

    if (b.getText() == ".") {
        result.getText() = result.getText() + b.getText()); ***
    }
}    

Thanks


Solution

  • In order to convert a charsequence to a String apply .toString() to the CharSequence Object and you should be fine to use + operators:

    [https://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html][1]

    Also it looks like you have a syntax error:

       result.getText() = result.getText() + b.getText()); *** the last ) is too much
    

    Furthermore you cannot write into getText() .. Getters (indicated by getXXX()) should be used to get values while Setters (indicated by setXXX()) should be used to store values into an object.

    result.setText(result.getText().toString() + b.getText().toString())