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.

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);



    }
}