Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Basic Android Programming Introduction to Java Objects

Solving problem of unavailability of setText (Char Sequence) in establishing OnClickListener.

A few videos before this, in the vid for setting up a OneClickListener, I kept getting a 'Cannot resolve method setText(Java.lang.string)' in which the only options available to me for autocomplete following setText were setTextAlignment and setTextDirection. There was no setText(Char )

Here is the non-working code:

public class IntimacyBuilderActivity extends Activity {

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

        //Declare our View variables and assign them the Views from the layout file
        final View intimacyQuestionLabel = findViewById(R.id.IntimacyQuestiontextView);
        Button showQuestionButton = (Button) findViewById(R.id.ShowQuestionbutton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //The button was clicked, so update the question label with a new question.
              String intimacyQuestion = "Would you like to be famous? In what way?";
              intimacyQuestionLabel.setText(intimacyQuestion);
            }
        };
        showQuestionButton.setOnClickListener(listener);

    }

I managed to resolve this problem by changing the code as I've pasted below, because I found a comment here http://stackoverflow.com/questions/19199946/the-method-settext-string-is-undefined-for-the-type-view saying that something was wrong with the static type of the reference:

"Methods are resolved according to the static type of the reference. findViewById() is declared with a return type of View and since the class View doesn't declare a method setText(), the compiler complains.

Use this

TextView msgto = (TextView)findViewById(R.id.to2);
msgto.setText(messageto2);

I thought the static type of the reference in the problem code might be View, and class View definitely wasn't declaring a method setText (only setTextAlignment and setTextDirection were available on auto-complete, unlike in the demo where setText (Char Sequence) was available), I changed my code to the following.

Now the OnClickListener button works as in the demo.

Thought you'd like to know (and maybe I just got lucky, this my first ever programming task ever) because it was very frustrating for me as a newbie since I followed the example exactly.

The successful code:

public class IntimacyBuilderActivity extends Activity {

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

        //Declare our View variables and assign them the Views from the layout file
        final TextView QuestionLabel = (TextView)findViewById(R.id.IntimacyQuestiontextView);
        Button showQuestionButton = (Button) findViewById(R.id.ShowQuestionbutton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //The button was clicked, so update the question label with a new question.
              String Question = "Who would you rather kiss? Putin or Jodorowsky?";
              //Randomly select a question

              //Update the label with our dynamic question
              QuestionLabel.setText(Question);

            }
        };
        showQuestionButton.setOnClickListener(listener);
    }

Thanks and I'd love to know if I did the right thing. Building an app for my partner for Christmas; this is my first time touching programming but this solution seems solid to me (I know, hubris) since it did seem that the View reference needed to include TextView, since setText (Char Sequence) wasn't available at all under merely the View reference, but it miraculously was after I added TextView.

N.B. I did streamline or simplify my labels from "intimacyQuestion" to "Question" in between the non-working code and arriving at the working code, but I ran a test with just the simplified labels and the original problem still obtained.

Ari loves Treehouse :)