1

I was wondering if anyone could tell me why:

TextView textblock = (TextView) findViewById(R.id.label).setText("Google is your friend.", TextView.BufferType.EDITABLE);

I get an undefined method error (setText is undefined for this type of view). However works when I do not chain e.g:

TextView textblock = (TextView)findViewById(R.id.label);
textblock.setText("Google is your friend.", TextView.BufferType.EDITABLE);

(I know this is a very basic question, however I am new to Java and could not find anything in my searches)

1
  • 4
    ((TextView) findViewById(R.id.label)).setText("..."); First cast to TextView and then set Text, have a look at my brackets
    – A.S.
    Oct 29, 2013 at 11:20

2 Answers 2

5

You need to call the method on the casted result, thanks to additional parentheses:

((TextView) findViewById(R.id.label)).setText("Google is your friend.", TextView.BufferType.EDITABLE);

That said, introducing a variable makes the code more readable. I would do that instead.

Also note that setText() returns void, and not the TextView. So you can't initialize a TextView variable with the result of setText() like you were trying to do.

0
0

setText() is of type void thus it doesnt return anything, which means you cant store in variable.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.