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

kelvin gwaruka
kelvin gwaruka
931 Points

Fun Facts app crashes after adding ID to relative Layout

I have followed Ben's exact steps in this video which are adding the relative layout ID and assigning the color change to red on button tap. I have the same code as his, with no errors in it.

However, when I run my app on the emulator it crashes. I am using my phone, HTC Desire 530 as the emulator as its much faster. I have tried to use the Google Nexus emulator after the app crashed on my phone numerously, but even that crashes as well. I'm assuming there must be some sort of conflict between my newly added code and the emulator. But have no idea how to resolve. Help.

Floris De Feyter
Floris De Feyter
17,174 Points

What does the error message say? Can you post a copy of both your FunFactsActivity.java and your activity_fun_facts.xml layout file?

Declan Smith
Declan Smith
453 Points

My Emulator has also crashed, I have tried changing the colour of the back ground, checked the content view and relativelayout. Still get an error message and the app not opening.

5 Answers

Floris De Feyter
Floris De Feyter
17,174 Points
setContentView(R.layout.activity_main);

should be:

setContentView(R.layout.activity_fun_facts);

You were telling the MainActivity to use an XML called "activity_main" in your second line of onCreate. The Activity will use this XML when you call findViewById() to search for the View you want (e.g. a Button). Since there is no View called "factTextView", "showFactButton" or "relativeLayout" in activity_main.xml (these views are in activity_fun_facts.xml), the findViewById() method will return null in each of these calls:

mFactTextView = (TextView) findViewById(R.id.factTextView);
mShowFactButton = (Button) findViewById(R.id.showFactButton);
mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

After clicking the button and your onClickListener is called, the app will immediately crash with a NullPointerException on this line:

mFactTextView.setText(fact);

because mFactTextView is null and hence you are calling a method on a null object.

Christian Rodriguez
Christian Rodriguez
Courses Plus Student 338 Points

my app crashes at that point too but only when i add the COLOR.RED ,my code is exactly like that one, how do i fix this?

kelvin gwaruka
kelvin gwaruka
931 Points

This is my FunFacts.java

package com.example.owner.funfacts;

import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity { private FactBook mFactBook = new FactBook(); private ColorWheel mColorWheel = new ColorWheel(); // Declare our view variables private TextView mFactTextView; private Button mShowFactButton; private RelativeLayout mRelativeLayout;

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

    // Assign the views from the layout file to the corresponding variables
    mFactTextView = (TextView) findViewById(R.id.factTextView);
    mShowFactButton = (Button) findViewById(R.id.showFactButton);
    mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);


    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
      String fact  = mFactBook.getFact();
      int Color = mColorWheel.getColor();


            //Update the screen with our dynamic fact
            mFactTextView.setText(fact);
            mRelativeLayout.setBackgroundColor(Color);
            mShowFactButton.setTextColor(Color);

        }
    };
    mShowFactButton.setOnClickListener(listener);

}

}

And this is my activity_fun_facts.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:layout="@+id/relativeLayout" tools:context="com.example.owner.funfacts.MainActivity" android:background="#51b46d">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Did you know?"
    android:id="@+id/TextView"
    android:background="#80ffffff" />

<Button
    android:text="Show Another Fun Fact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="23dp"
    android:id="@+id/showFactButton"
    android:background="@android:color/white"
    android:textColor="#51b46d"/>

<TextView
    android:text="Ants stretch when they wake up in the morning"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:textColor="@android:color/white"
    android:id="@+id/factTextView"
    android:layout_below="@+id/TextView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="157dp" />

</RelativeLayout>

This the message I get in my messages log after running the code;

Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources, :app:assembleDebug] Information:BUILD SUCCESSFUL Information:Total time: 36.451 secs Information:0 errors Information:0 warnings Information:See complete output in console

And the screen on the emulator just reads unfortunately, Fun Facts has stopped, after I click the Show Another Fun Fact button.

kelvin gwaruka
kelvin gwaruka
931 Points

Thanks for your answer Floris, So I changed

setContentView(R.layout.activity_main); to setContentView(R.layout.activity_fun_facts); through rename>refactor.

But Still my Fun Facts App crashes. I don't think that my app should crash on mFactTextView.setText(fact); because I have the facts referred to by the above method stored in a Factbook.java.

Also, I don't think that the mFactTextView is null because there is a factTextView ID in the view defined below.

<TextView android:text="Ants stretch when they wake up in the morning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:textColor="@android:color/white" android:id="@+id/factTextView" android:layout_below="@+id/TextView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="157dp" />

If I am wrong Floris, please what steps do you think I should take to correct this problem?

kelvin gwaruka
kelvin gwaruka
931 Points

Thanks Flores , I have finally resolved the issue .

Floris De Feyter
Floris De Feyter
17,174 Points

Hi Kelvin! Great! I'm glad you figured it out! The steps you answered above didn't work because you didn't need refactor > rename here. The problem is not that the activity_main.xml has a wrong name, the problem is that you don't need it over there. So you don't need to use refactor > rename, you should just change setContentView(R.layout.activity_main); to setContentView(R.layout.activity_fun_facts); by just changing the text. When you use setContentView(R.layout.activity_main);, you will use activity_main.xml (which probably doesn't contain the line android:id="@+id/factTextView") to look for your views. When you use setContentView(R.layout.activity_fun_facts);, however, you will use activity_fun_facts.xml (which DOES contain the line android:id="@+id/factTextView") to look for your views. The latter case will work, the former won't.

Pablo Ocampo solis
Pablo Ocampo solis
748 Points

I have the same problem with the dynamic background how do I solve it? when I comment that the app works fine help!!!