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 Build a Simple Android App Improving Our Code Dynamically Changing the Background Color

My emulator says " Unfortunately , App has stopped ". And the error is on line "mRelativeLayout..."

Just can't find the problem

1 Answer

Grynt Logmadrssen
Grynt Logmadrssen
2,556 Points

I was getting the same error, but I changed all layout instances from relative to constraint in both the XML and FunFactsActivity.java files, and now it's working for me.

Just the opposite for me. The activity type we choose at the beginning (Android Studio 2.3.3) defaults to android.support.constraint.ConstraintLayout . Not relative like the videos.

Where the instructor used RelativeLayout I used ConstraintLayout.
I also used mBGColor for my name, that is just my preference.
appBody is the id I gave the layout in the XML

android:id ="@+id/appBody"

so I could find it by Id.

public class FunFactsActivity extends AppCompatActivity {

    // Create Instance or FactBook for use in OnClick
    private FactBook mFactBook = new FactBook();
    //Declare View Variables
    private TextView mFactTextView;
    private Button mShowFactButton;
    private ConstraintLayout mBGColor;



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

        // Assign views to variables
        mFactTextView = (TextView) findViewById(R.id.funfactTextView);
        mShowFactButton = (Button) findViewById(R.id.showFactButton);
        mBGColor = (ConstraintLayout) findViewById(R.id.appBody);

        //Setup OnClick
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            String fact = mFactBook.getFact();
                // Update the screen with the fact and change color
                mFactTextView.setText(fact);
                mBGColor.setBackgroundColor(Color.RED);

            }
        };

        mShowFactButton.setOnClickListener(listener);



    }
}