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 trialGustavo Rocha
14,151 PointsApplication 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
UX Design Techdegree Graduate 22,796 PointsHello 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