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

Timothy Van Cauwenberge
Timothy Van Cauwenberge
8,958 Points

Problem with Android Fun Facts code

Its giving me an error where it says factTextView = findViewById(R.id.factTextView); and the line below that. It says Cannot resolve problem (factTextView). The class isn't done in workspaces so I have to copy and paste.

package com.example.timmy.funfacts;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FunFactsActivity extends AppCompatActivity {
    //private FactBook factBook = new FactBook();
    // declare variables
    private TextView factTextView;
    private Button showFactButton;
    //private RelativeLayout relativelayout;


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

        // Assign the views from the layout file to the corresponding variables
        factTextView = findViewById(R.id.factTextView);
        showFactButton = findViewById(R.id.showFactButton);

        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                String fact = factBook.getFact();
                // update the screen with new fact
                factTextView.setText(fact);
            }
        };

        showFactButton.setOnClickListener(listener);
    }
}

Moderator edited: Markdown added so that code renders properly in the forums.

I think you have to cast the views like so:

factTextView = (TextView) findViewById(R.id.factTextView); showFactButton = (Button) findViewById(R.id.showFactButton);

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Try letting Android do the cast for you. Android Studio provides several context menu options for doing this

ctrl + shift + o

Failing that, it might simply be that you need to rebuild or "clean" your project. This is known as syncing and it makes sure that you build.gradle file is up to date.

Good luck! :)

Timothy Van Cauwenberge
Timothy Van Cauwenberge
8,958 Points

When i do that it just says TextView is redundant