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

Java

The fact part of my code is greyed out and it says it cant be used?

This is my code...

package com.example.voltron.myapplication;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends Activity {

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

        final TextView factlabel = (TextView) findViewById(R.id.FactTextView);
        Button showfactbutton = (Button) findViewById(R.id.ShowFactButton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fact = "";
                factlabel.setText(fact);
                Random randomGenerator = new Random();
                int randomNumber = randomGenerator.nextInt(3);
           if (randomNumber == 0) {

               fact = "Its going very slow....";

Not sure why but the fact -= "its going very slow... says I cant use fact for some reason. Anybody know?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey William,

welcome to Java forum :)

I think this question is not right here and should go to ANDROID topic...

Am I right?

But you are very very welcome here :)

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey William,

Like Grigorij said, this should be in the Android section

Right off the bat, I see you used the keyword "final". This means your textview cannot be changed. However in your onclick listener, you are changing the text on the textview. See how that doesn't make sense?

Secondly, I believe you are trying to call that listener when the button is clicked. Therefore you should change

View.OnClickListener listener = new View.OnClickListener()

to

showFactButton.setOnClickListener(new View.OnClickListener()