0

this is my code to set a value to a TextView

enter code here

public void addHours(View view){
    final TextView mTextView = (TextView) findViewById(R.id.newHours_Value);
    String hourValue = R.id.enterHours.getText().toint();
    mTextView.setText("hourValue");
}

but the R.id.enterHours.GetText() comes up with an error

Cannot invoke getText() on the primitive type int

what am i doing wrong please.

Graham
  • 1
  • 2

2 Answers2

4

This

 R.id.enterHours

returns an int. That's why when you initialize a View you do it like

TextView mTextView = (TextView) findViewById(R.id.newHours_Value);

The (TextView) casts it to a TextView so you can call its methods on it like getText().toString(). So what you probably want is

TextView hourValueTV = (TextView) findViewById*R.id.enterHours);  //assuming its a TextView and not EditText
String hourValue = hourValueTV.getText().toString();
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

Adding to what codeMagic said:

In this case, you should set text with the variable name (reference) of the String and not value ("hourValue", unless you want your text to be hourValue).

It should be

  mTextView.setText(hourValue);
  • Many thanks all, i did note the other questions but could not understand the answers, i am a newbee to android programming – Graham Sep 11 '13 at 08:25