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 (2014) Testing and Debugging Toast Notifications

App wont start please help

When i try to run the app it says "Unfortunately, Fun Facts has stopped". I don't know why here is my client code and then my LogCat Client: package com.lukan.funfacts;

import java.util.Random;

import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.os.Build;

public class FunFactsActivity extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        // Declare view variable
        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() {
            public void OnClick(View view) {
                String fact = mFactBook.getFact();

                factLabel.setText(fact);

                int color = mColorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                showFactButton.setTextColor(color);
            }

            @Override
            public void onClick(View v) {

            }

        };
        showFactButton.setOnClickListener(listener);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fun_facts, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_fun_facts,
                container, false);
        return rootView;
    }
}

}

HERE IS MY LogCat: 04-29 11:36:05.013: I/Process(1229): Sending signal. PID: 1229 SIG: 9 04-29 11:36:05.573: D/AndroidRuntime(1269): Shutting down VM 04-29 11:36:05.573: W/dalvikvm(1269): threadid=1: thread exiting with uncaught exception (group=0xb3b04ba8) 04-29 11:36:05.593: E/AndroidRuntime(1269): FATAL EXCEPTION: main 04-29 11:36:05.593: E/AndroidRuntime(1269): Process: com.lukan.funfacts, PID: 1269 04-29 11:36:05.593: E/AndroidRuntime(1269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lukan.funfacts/com.lukan.funfacts.FunFactsActivity}: java.lang.NullPointerException 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.ActivityThread.access$800(ActivityThread.java:135) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.os.Handler.dispatchMessage(Handler.java:102) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.os.Looper.loop(Looper.java:136) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.ActivityThread.main(ActivityThread.java:5017) 04-29 11:36:05.593: E/AndroidRuntime(1269): at java.lang.reflect.Method.invokeNative(Native Method) 04-29 11:36:05.593: E/AndroidRuntime(1269): at java.lang.reflect.Method.invoke(Method.java:515) 04-29 11:36:05.593: E/AndroidRuntime(1269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 04-29 11:36:05.593: E/AndroidRuntime(1269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-29 11:36:05.593: E/AndroidRuntime(1269): at dalvik.system.NativeStart.main(Native Method) 04-29 11:36:05.593: E/AndroidRuntime(1269): Caused by: java.lang.NullPointerException 04-29 11:36:05.593: E/AndroidRuntime(1269): at com.lukan.funfacts.FunFactsActivity.onCreate(FunFactsActivity.java:55) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.Activity.performCreate(Activity.java:5231) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 04-29 11:36:05.593: E/AndroidRuntime(1269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 04-29 11:36:05.593: E/AndroidRuntime(1269): ... 11 more 04-29 11:36:08.333: I/Process(1269): Sending signal. PID: 1269 SIG: 9

2 Answers

Hello,

The exception has been happening because you have all of your layout information in fragment_fun_facts.xml when you are expanding activity_fun_facts.xml in your code. So if you rename yoru fragment_fun_facts.xml to activity_fun_facts.xml(backup your old activity_fun_facts.xml first) and remove the line tools:context="com.lukan.funfacts.FunFactsActivity$PlaceholderFragment" you should be a lot closer to a working application. Again, the reason for this is that in your java files, you're using setContentView(R.layout.activity_fun_facts); which is looking in activity_fun_facts.xml for all of your layout information, so that when you call final Button showFactButton = (Button) findViewById(R.id.showFactButton); it is not finding a showFactsButton, so the function is returning null. At which point when you call showFactButton.setOnClickListener, you are trying to call a method on a null object and thus getting the exception.

Hello,

You appear to have two methods in your OnClickListener named onClick, one of them is empty. I'd recommend adding the @Override notation to the top of the first one and remove the second, so that you end up with

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void OnClick(View view) {
                String fact = mFactBook.getFact();

                factLabel.setText(fact);

                int color = mColorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                showFactButton.setTextColor(color);
            }
        };

This might not be your only issue, but it is one that could cause some unforseen issues to occur.

Caused by: java.lang.NullPointerException 04-29 11:36:05.593: E/AndroidRuntime(1269): at com.lukan.funfacts.FunFactsActivity.onCreate(FunFactsActivity.java:55)

The only other thing in this line that could be null and cause a probleme would be the showFactButton. However, that could only be null if the button's id isn't showFactButton(in other words if it doesn't end up being R.id.showFactButton).

When i get rid of the: public void onClick(View v) { // TODO Auto-generated method stub

            }

I Get and error at the first Onclick method saying there is unimplemented methods

Opps, I missed the difference in capitalization. Change your OnClick to be onClick

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fact = mFactBook.getFact();

                factLabel.setText(fact);

                int color = mColorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                showFactButton.setTextColor(color);
            }
        };

Okay so i got rid of the method without causing an error but i think there is something wrong with line 52:

showFactButton.setOnClickListener(listener);

Its looking like showFactButton might be null. I'd recommend some more cleanup of code that you aren't using though to simplify things so they're easier to read(and might elimininate a bug in the process). You should be able to get rid of the whole PlaceHolderFragment class

Then change:

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        // Declare view variable
        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() {
            public void OnClick(View view) {
                String fact = mFactBook.getFact();

                factLabel.setText(fact);

                int color = mColorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                showFactButton.setTextColor(color);
            }

            @Override
            public void onClick(View v) {

            }

        };
        showFactButton.setOnClickListener(listener);

    }
}

to

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

     // Declare view variable
     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();

             factLabel.setText(fact);

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

What this change does is make it so you're not expanding out a fragment that isn't being used and brings you more in line to what the video is showing. However, if this doesn't help, I'd have to see your activity_fun_facts.xml file to provide any further assistance.

Here is the activity_fun_facts.xml file: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.lukan.funfacts.FunFactsActivity" tools:ignore="MergeRootFrame" />

And here is the fragment_fun_facts.xml file: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/Green" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.lukan.funfacts.FunFactsActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Did you know?"
    android:textColor="@color/transWhite"
    android:textSize="24sp" />

<TextView
    android:id="@+id/factTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_centerVertical="true"
    android:text="Ants stretch when they wake up in the morning."
    android:textColor="@color/White"
    android:textSize="24sp" />

<Button
    android:id="@+id/showFactButton"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@color/White"
    android:text="Show Another Fun Fact" 
    android:textColor="@color/Green"/>

</RelativeLayout>