1. Download our Official Android App: Forums for Android!

Apps No applicable method to float

Discussion in 'Android Development' started by DevHenrik, Dec 23, 2014.

  1. DevHenrik

    DevHenrik Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Dec 20, 2014

    Dec 20, 2014
    9
    0
    5
    How do I solve this?
    It sais: "There is no applicable method to float" , maybe not right spelled...
    My code is like this and the error shows on last line at "calc"

    The float numbers is connected to EditText widgets.

    public void CalcButton(View button) {
    float no1 = Float.parseFloat(fp.getText().toString());
    float no2 = Float.parseFloat(fC.getText().toString());
    float no3 = Float.parseFloat(drive.getText().toString());
    float calc = (no1*no2)*no3;
    totalcost.setText(calc);
    }
     

    Advertisement

  2. Manaya

    Manaya Member
    Rank:
    None
    Points:
    18
    Posts:
    62
    Joined:
    Aug 4, 2014

    Aug 4, 2014
    62
    14
    18
    You passed a float to setText when it needs a text instead (string or array of chars I think)
     
  3. DevHenrik

    DevHenrik Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Dec 20, 2014

    Dec 20, 2014
    9
    0
    5
    It works like this with int... but when I do it with float it shows error! :/
     
  4. Manaya

    Manaya Member
    Rank:
    None
    Points:
    18
    Posts:
    62
    Joined:
    Aug 4, 2014

    Aug 4, 2014
    62
    14
    18
    Because there is a setText(int resid) method so it won't show any error, however I don't think it would do the expected behavior you want since from what I see you want to show the number result of a calculation? While the resid provides an int which represents the id of a string in your xml.

    Try look in the original Android documentation for possible setText methods:
    http://developer.android.com/reference/android/widget/TextView.html

    You must either use a CharSequence or the setText (char[] text, int start, int len) option, and convert your float to a string first.
     
  5. DevHenrik

    DevHenrik Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    9
    Joined:
    Dec 20, 2014

    Dec 20, 2014
    9
    0
    5
    I got it working... by this code public void CalcButton(View button) {

    String n1 = fp.getText().toString();
    float no1 = Float.parseFloat(n1);

    String n2 = fC.getText().toString();
    float no2 = Float.parseFloat(n2);

    String n3 = drive.getText().toString();
    float no3 = Float.parseFloat(n3);


    float calc = no1*(no2*no3);

    String sum = Float.toString(calc);
    totalcost.setText(sum);


    }
     
  6. Manaya

    Manaya Member
    Rank:
    None
    Points:
    18
    Posts:
    62
    Joined:
    Aug 4, 2014

    Aug 4, 2014
    62
    14
    18
    Yup, that's what I was talking about, glad you solved it.
     

Share This Page

Loading...