0

this is my MainActivity.java

package com.calc.calculator;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {
private EditText no1=(EditText) findViewById(R.id.editText1);
private EditText no2=(EditText) findViewById(R.id.editText2);
private EditText out=(EditText) findViewById(R.id.editText3);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void add(View view){
    float output=Float.parseFloat(out.getText().toString());
    float no1Value = Float.parseFloat(no1.getText().toString());
    float no2Value = Float.parseFloat(no2.getText().toString());
    float ans=Calc.Add(no1Value, no2Value);
    output.setText(String.valueOf(Calc.Add(no1Value, no2Value)));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

this is my Calc.java

package com.calc.calculator;

public class Calc {

public static float Add(float no1,float no2){
    return(no1+no2);
}

}

i am trying to make a very simple app that calculates the sum of two nos. but it shows an error in Mainactivity.java at line 35 which is

output.setText(String.valueOf(Calc.Add(no1Value, no2Value)));

it says Cannot invoke setText(String) on the primitive data type float

i am new to android development and i am stuck on this very beginner problem. pls help :D

3
  • 3
    output is defined as a float (float output=Float.parseFloat(out.getText().toString());). You can't pass a string to it. It doesn't have the setText() method. Commented Dec 13, 2014 at 18:45
  • Please check this answer: here Commented Dec 13, 2014 at 18:50
  • That's a typo neither java nor android error. Better check your code instead of putting an answer. You should have written out.setText(String.valueOf(Calc.Add(no1Value, no2Value))); Commented Dec 13, 2014 at 18:56

1 Answer 1

1

You are trying to convert output of type float to a String

change output to type String

or String output_ = "" + output;

and then

out.setText(output_);

Sign up to request clarification or add additional context in comments.

5 Comments

@SanilKhurana view Edit
i tried that now i am getting settext is undefeined for string
@SanilKhurana what are you trying to set the text of
i am trying to make the output editview show the result of the function Add in Calc.java
that worked. your previous edit was correct too, i just made a typo and when i saw the error icon i pressed ctrl+z thinking that the error was not resolved.srry about that. and thnx for d help :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.