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!

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 (2014) Testing and Debugging Running the App on a Device

When running on phone it comes up with "Unfortunately, Fun Facts has stopped."

I have gone through my code and the debugger doesn't come up with any errors. I have connected my phone and the app has loaded but it only shows the first screen, when you tap the 'next' button it closes with a message "Unfortunately, Fun Facts has stopped"

can you provide a copy of your code?

package com.rendeevoo.tessa.funfacts;

import android.app.Activity; import android.graphics.Color; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; 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 android.widget.Toast;

public class FunFactsActivity extends Activity {

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 the Views from the layout file
    final TextView txtfact = (TextView) findViewById(R.id.txtfact);
    final Button btnnext = (Button) findViewById(R.id.btnnext);

    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 the label with our dynamic fact
            txtfact.setText(mFactBook.getFact());

            int color =mColorWheel.getColor();
            relativeLayout.setBackgroundColor(color);
            btnnext.setTextColor(color);
        }
    };
    btnnext.setOnClickListener(listener);


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

}

}

Not sure if this is the right one

10 Answers

final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); FOR THE LINE ABOVE, TRY CLOSING THE GAP BETWEEN ) AND FINDVIEW.... DO THE SAME FOR BUTTON AS WELL.

THIS CODE: View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) {

SHOULD HAVE () LIKE: mSignUpTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { DISREGARD mSignUpTextView.setOnClickListener, JUST PART OF MY CODE. BUT THE REST SHOULD LOOK THE SAME.

PUT A SPACE BETWEEN THE = SIGN: int color =mColorWheel.getColor();

TRY THOSE AND SEE IF THAT HELPS ANY.

Thank you LeJacque Gillespie but it is still coming up with the message when running the app on the phone. Is there anything else I should be doing?

Did you go to the BUILD tab and click on rebuild project and or clean project?

I've just done both rebuild and clean - running the app after each finished and it still came up with the same message.

Are you seeing any errors in your logcat? If not you might have to start over from scratch. I had to do that once or twice myself and the second time they worked fine.

This is what is red in my logcat:

05-11 15:31:18.570 32226-32226/com.rendeevoo.tessa.funfacts E/AndroidRuntimeοΉ• FATAL EXCEPTION: main Process: com.rendeevoo.tessa.funfacts, PID: 32226 java.lang.NullPointerException: Attempt to get length of null array at com.rendeevoo.tessa.funfacts.ColorWheel.getColor(ColorWheel.java:37) at com.rendeevoo.tessa.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:41) at android.view.View.performClick(View.java:4832) at android.view.View$PerformClick.run(View.java:19839) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5317) 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:1016) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811) device offline

look like yo may have errors on line 37 and 41, go back over those.

com.rendeevoo.tessa.funfacts.ColorWheel.getColor(ColorWheel.java:37) at com.rendeevoo.tessa.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:41)

I think you might be missing (). Check the syntax on those lines.

also make sure you have all of your braces {} closed, somethimes we throw Null Pointer Exceptions when all of our braces aren't closed. check the entire code. You can download a copy of the code used in the course from the bottom as well. Click on the downloads tab on the video of the particular course and download the first file. Unzip it and go into the src folder, from there navigate till you get to the codes. It's normally four files named MainActivity and so on, just like the files in android. Check you braces off of that file.

should there be any other ()?

for the ColorWheel.... int randomNumber = randomGenerator.nextInt(mFacts.length); for the FunFactsActivity... int color = mColorWheel.getColor();

Maybe not, I'm thinking it's a braces {} issue. Double check all of them, before and after your methods to include class methods. They are easy to overlook.

Thank you for your help LeJacque Gillespie I have worked it out and it now works.

Awesome.