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

Ruslan T
Ruslan T
1,706 Points

Cannot resolve "factTextView"

I copied the code as it is shown in the video, but I get this error:

Cannot resolve "factTextView". in

factTextView = findViewById(R.id.factTextView);

Here's my java code:

package com.example.admin.facts;

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

public class funnestActivity extends AppCompatActivity {

    private TextView factTextView;
    private Button factButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funnest);
        factTextView = findViewById(R.id.factTextView);
    }
}

and here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#51b46d"
    tools:context=".funnestActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Did you know?"
        android:textColor="#84ffffff"
        android:textSize="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.059"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.029" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="333dp"
        android:layout_height="249dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="64dp"
        android:background="#2e000000"
        android:padding="10dp"
        android:shadowDx="5"
        android:shadowRadius="0"
        android:text="7% of all American adults believe that chocolate milk comes from brown cows. That works out to 16.4 million people."
        android:textColor="@android:color/white"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="44dp"
        android:layout_marginBottom="32dp"
        android:layout_marginEnd="38dp"
        android:layout_marginStart="38dp"
        android:background="@android:color/white"
        android:text="Give me a new fact!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

I've tried cleaning and rebuilding the project and checking the R resources, but nothing seems to work.

I need help fast, no one has answered my previous question for over a week and I can't continue the Android track because of this.

1 Answer

Abdallah Al Sayed Ali Saf
Abdallah Al Sayed Ali Saf
529 Points

First factTextView id doesn't exist mate you have to put it in the id property of a TextView

     ```xml

       <TextView
    android:id="@+id/factTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Did you know?"
    android:textColor="#84ffffff"
    android:textSize="24dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.059"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.029" />

      ```

Secondly cast :

      ```java

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

      ```