Sanil Khurana
Sanil Khurana

Reputation: 1169

can resolve error in very basic android code

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

Upvotes: 0

Views: 64

Answers (1)

SemperAmbroscus
SemperAmbroscus

Reputation: 1398

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_);

Upvotes: 1

Related Questions