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 (retired 2014) Getting Started with Android Java Variables Explained

How can I solve this error answer.setText(answer);

Please how can I solve the error: answer.setText(answer); because it is not making my Crystal Ball emulator run.. I need help.


with an answer String answer = "Yes"; answer.setText(answer);
} }); }

@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;
}

3 Answers

I think you need to add these lines of code: ***Java String answer = "";

            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(3);
            answer = Integer.toString(randomNumber);

            if (randomNumber == 0){
                answer = "Yes!";
            }
            else if (randomNumber == 1){
                answer = "No";
            }
            else if (randomNumber == 2){
                answer = "Maybe.";
            }
            else{
                answer = "Sorry, there has been an error.";
            }

            answerLabel.setText(answer);

        }
    });
}

@michaelpavle Thanks for your help but this is the complete program but it is always showing there is error here : answerLabel.setText(answer); .. It shows red underline and I don't know how to fix it

package com.puritywordview.crystalgaze;

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.Button;

public class MainActivity<TextView> extends ActionBarActivity {

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

    // Declare our View variables and assign them the views from the layout file
    final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = null;

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // The button was clicked, so update the answer label with an answer
            String answer = "Yes";
             answer.setText(answer);        
        }
    });
}


@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;
}

@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);
}

}

I need help on it seriously

When I checked the code, you wrote answer.setText(answer); and not answerLabel.setText(answer);