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 trialSonia Reeder-Jones
3,785 PointsR.id and R.layout are red in my Fun Facts Activity java file and are producing errors.
I'm getting a "cannot resolve symbol R" message in the Fun Facts Activity java file and the build fails. All R symbols are marked in red including R.layout and R.id. There is no R.id import statement. Some sources suggest doing Build>Clean Project, but this does not solve the problem. Other sources suggest adding an R.id import statement, but manually adding this leaves it gray or "unused". How do you correct this?
Sonia Reeder-Jones
3,785 PointsHere is the code:
import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class FunFactsActivity extends AppCompatActivity { // Declare our view variables private TextView mFactTextView; private Button mShowFactButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
// Assign the Views from the layout files to the corresponding variables
mFactTextView - (TextView) findViewById(R.id.factTextView);
mShowFactButton = (Button) findViewById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// The button was clicked, so update the fact TextView with new fact String fact = "This is a fun fact"; mFactTextView.setText(fact); } };
mShowFactButton.setOnClickListener(listener);
}
public void setFactTextView(TextView factTextView) {
mFactTextView = factTextView;
}
}
3 Answers
Seth Kroger
56,414 PointsThe most likely cause is an error in your layout file. R is a special kind of class because it's generated for your project whenever you build. Anything is res is scanned and given a resource id. An error in a res file, like an activity layout would make R fail to generate, causing your errors. If you go back to the layout file in text view, you should see red underlines there to fix.
Sonia Reeder-Jones
3,785 PointsThank you for your response. I've checked the layout file and I don't see any errors. Any other suggestions?
Joyce Chidiadi
1,867 PointsCan you check your naming for the TextView id in your xml file? Does this error also occur on the showFactButton id?
Pablo Zirilli
Courses Plus Student 3,327 PointsI had the same issue... solved change this:
mShowFactButton = (Button) findViewById(R.id.showFactButton);
by
mShowFactButton = (Button) findViewById(R.id.ShowFactButton);
The capital "S" on ShowFactButton id.
Let me know if this helps you. Grettings
Joyce Chidiadi
1,867 PointsJoyce Chidiadi
1,867 PointsHi Sonia,
Can you past your code and gradle build script (app) here let's have a look