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

Fun Facts Keeps Crashing!

Please Help. I have updated Android Studio, and have troubleshooted the Internets to no end. Here is the code which I followed step by step in the tutorial:

package com.example.dw.funfacts;

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

import static android.view.View.OnClickListener;

public class FunFactsActivity extends AppCompatActivity {

    public static final String TAG = FunFactsActivity.class.getSimpleName();

    private FactBook mFactBook = new FactBook();
    private ColorWheel mColorWheel = new ColorWheel();

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

        // Declare our view variables and assign them the Views from the layout file
        final TextView factLabel = (TextView) findViewById(R.id.factTextView);
        final Button showFactButton = (Button) findViewById(R.id.showFactButton);
        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

         OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {

                String fact = mFactBook.getFact();

                //Update the label with our dynamic fact
                factLabel.setText(fact);
                int color = mColorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                showFactButton.setText(color);
                }
            };
        showFactButton.setOnClickListener(listener);

        //Toast.makeText(this, "Yay! Our activity was created!", Toast.LENGTH_LONG).show();
        Log.d(TAG, "We're logging from the onCreate() method!");

    }

}

LOGCAT:

09-30 19:16:52.521    2265-2265/com.example.dw.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.dw.funfacts, PID: 2265
    android.content.res.Resources$NotFoundException: String resource ID #0xffe0ab18
            at android.content.res.Resources.getText(Resources.java:299)
            at android.widget.TextView.setText(TextView.java:4132)
            at com.example.dw.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:40)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

What is wrong?

Thanks!

3 Answers

Hello,

In your error message, the first reference to something in your project is

            at com.example.dw.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:40)

which, line 40 is

showFactButton.setText(color);

Which is trying to change the text of the showFactButton to the contents of color. However, color is an int, so Android is trying to then map that int to a string resource, which you have none available so you are getting an Exception.

The fix is going to depend upon what you are really wanting to do with that line. If I remember the project right, you might not even need that line at all. I'd suggest commenting it out and seeing if that works for you. Please let us know if you need more help. If you do need more help, please let us know what you changed so that we can help you from where you are at.

That was the problem. Thank You!

The problem is the line:

 showFactButton.setText(color);

because text and color aren't really the same thing.

Thank You!

If I remember this app correctly, you are trying to set the text of the button to be the same as the background color. The code you use is setting the text of the button equal to a color. I believe you will need to change the setText to something else that deals with setting the text color instead.

Thanks!