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

Android Fun facts " Unfortunately, Fun Facts has stopped." Error

A lot of people had this issue and I couldn't find a solution to this from other posts. I get the error after a few facts are displayed.

Here are the files:

colorwheel.java:

package com.iamakash.funfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by akash on 2015-07-26.
 */
public class colorwheel {

    public String[] mcolors = {
            "#39add1",
            "#3079ab",
            "#c25975",
            "#e15258",
            "#f9845b",
            "#838cc7",
            "#7d669e",
            "#53bbb4",
            "#51b46d",
            "#e0ab18",
            "#637a91",
            "#f092b0",
            "#b7c0c7",
            "#4169E1",
            "#F0F8FF",
            "#FFEBCD",
            "#F5F5DC",
            "#FFE4C4 ",
           " #000000",
            "#FFEBCD",
           " #0000FF",
           " #8A2BE2",
    };
    public int getcolor(){

        //button clicked, update with new fact
        String COLOR = "";
        //randomly select fact
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(mcolors.length);
        COLOR=mcolors[randomNumber];
        int colorAsInt= Color.parseColor(COLOR);
        return colorAsInt;
    }


}

factbook.java:

import java.util.Random;


public class factBook {

    public String[] mfacts = {
            "Ants stretch when they wake up in the morning.",
            "Ostriches can run faster than horses.",
            "Olympic gold medals are actually made mostly of silver.",
   "You are born with 300 bones; by the time you are an adult you will have 206.",
         "It takes about 8 minutes for light from the Sun to reach  Earth.",
       "Some bamboo plants can grow almost a meter in just one day.",

    };
    public String getFact(){

    //button clicked, update with new fact
    String fact = "";
    //randomly select fact
    Random randomGenerator = new Random();
    int randomNumber = randomGenerator.nextInt(mfacts.length);
    fact=mfacts[randomNumber];
    return fact;
    }


}

MainActivity.Java:

import android.app.Activity;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.*;


public class FunFactsActivity extends Activity {
    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 view variables

    final TextView factlabel = (TextView) findViewById(R.id.facttextView);
    final Button showFactButton = (Button) findViewById(R.id.showfactbutton);
        final RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
        View.OnClickListener Listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fact= mFactBook.getFact();



                //update label with dynamic fact

                factlabel.setText(fact);
                int color=mcolorwheel.getcolor();
                relativeLayout.setBackgroundColor(color);



            };
        };


    showFactButton.setOnClickListener(Listener);
};



    };

Here are the Logcat errors:

java.lang.IllegalArgumentException: Unknown color
        at android.graphics.Color.parseColor(Color.java:225)
        at com.iamakash.funfacts.colorwheel.getcolor(colorwheel.java:44)
        at com.iamakash.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:44)
        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)

1 Answer

Some of your 'mcolors' values have spaces in them. I would try removing those.

These: "#FFE4C4 ", " #000000", " #0000FF", " #8A2BE2",

Sorry for the late reply! Thanks a lot, your idea worked! Another quick question. How do you think I would go about printing the facts but making them not repeat until all of them have been displayed. The random function sometimes repeats the same one