Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Gustavo Rocha
Gustavo Rocha
14,151 Points

Application crashes when i open it The emulator shows "Unfortuately, Fun Facts has stoped"

package com.example.funfacts;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView;

import java.util.Random;

import static com.example.funfacts.R.id.funFactLayout;

public class MainActivity extends AppCompatActivity { //Declare our fields variables private TextView factTextView; private Button showFactButton; private RelativeLayout relativeLayout; private FactBook factBook = new FactBook();

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

    // Assign the Views to the corresponding varibles
    factTextView = findViewById(R.id.factTextView);
    showFactButton = findViewById(R.id.showFactButton);
    relativeLayout = findViewById(funFactLayout);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String fact = factBook.getFact();

            //Update the screen with our new fact
            factTextView.setText(fact);
            relativeLayout.setBackgroundColor(Color.RED);

        }
    };
    showFactButton.setOnClickListener(listener);
}

}

1 Answer

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,795 Points

Hello Gustavo,

I think you forgot to search the relative layout by it id, so the System doesn't know what RelativeLayout is and can't find it.

//your code
 relativeLayout = findViewById(funFactLayout);

// try this
 relativeLayout = findViewById(R.id.funFactLayout);

Cheers