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) Basic Android Programming Adding the onClick() Method

Timoor Alshunag
PLUS
Timoor Alshunag
Courses Plus Student 788 Points

Error while running the app I'm getting a error while running the app "unfortunately Fun facts has stopped"

package com.teamtreehouse.funfacts;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class FunFactsActivity extends Activity {

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

    // Declare our view variable and assign the views from the layout file
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showfactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          // The button was clicked, so update the fact label with new fact
            String fact = "Ostriches can run faster than horses.";
            factLabel.setText(fact);
        }
    };
    showfactButton.setOnClickListener(listener);

4 Answers

it may be because you didn't declare/assign your relativeLayout. See if this works, add this to your view variables.

// Declare our view variables and assign the views from the layout file.
...
...
        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); //add this variable

Dont forget to add it to your list of imports

import android.widget.RelativeLayout;

I, too, continue to get this error message. "Unfortunately, Fun Facts has stopped." I tried the above mentioned fix but still no luck.

@mitchellstockton Besides your force close message inside the emulator. In android studio, at the time of the crash, is there any error message being shown? The force close isn't enough of an error message to tell why your app is crashing .. The message will be somewhere at the bottom of android studio where the console is and such.

And usually the error messages are pretty specific as to why your app continues to crash.

This is what it shows when it crashes:

03-09 10:15:50.069 2316-2316/com.example.jenny.funfacts I/art: Not late-enabling -Xcheck:jni (already on) 03-09 10:15:50.154 2316-2316/com.example.jenny.funfacts W/System: ClassLoader referenced unknown path: /data/app/com.example.jenny.funfacts-2/lib/x86 03-09 10:15:50.182 2316-2316/com.example.jenny.funfacts D/AndroidRuntime: Shutting down VM 03-09 10:15:50.183 2316-2316/com.example.jenny.funfacts E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jenny.funfacts, PID: 2316 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jenny.funfacts/com.example.jenny.funfacts.FunFactsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2090) at com.example.jenny.funfacts.FunFactsActivity.<init>(FunFactsActivity.java:14) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1067) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Timoor Alshunag
PLUS
Timoor Alshunag
Courses Plus Student 788 Points

I recieve this massage when i try to run the emulator:

03-05 15:09:58.764 1108-1108/com.teamtreehouse.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.teamtreehouse.funfacts, PID: 1108 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teamtreehouse.funfacts/com.teamtreehouse.funfacts.FunFactsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.teamtreehouse.funfacts.FunFactsActivity.onCreate(FunFactsActivity.java:31) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)             at android.app.ActivityThread.access$800(ActivityThread.java:135)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5001)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)             at dalvik.system.NativeStart.main(Native Method) 03-05 15:10:02.964 1108-1108/com.teamtreehouse.funfacts I/Process﹕ Sending signal. PID: 1108 SIG: 9

@Timoor Alshunag from looking at the error log you posted, it shows that you have a NullPointerException in your FunFactsActivity class and that's what is causing your app to crash.

A NullPointerException means somewhere in your code is pointing to a Null value, a Null value is a value with nothing in it.

If you want you can post your code here or just go through the lesson again and make sure you didn't make any syntax errors and that your copied the code correctly, and didn't leave any out.

I can't find the fault in my code. Here's what I have:

package com.example.jenny.funfacts;

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;

public class FunFactsActivity extends AppCompatActivity { // Declare our View variables private TextView mFactTextView; private Button mShowFactButton; final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); // add this variable

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

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

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // The button was clicked, so update the fact TextView with a new fact
            String fact = "Ostriches can run faster than horses.";
            mFactTextView.setText(fact);
        }
    };
    mShowFactButton.setOnClickListener(listener);
}

}

I don't see any errors within your code. Post your log cat so I can see what it says.

@derekmarkman, you asked me to post my log cat and here it is: 03-09 10:26:27.563 3190-3190/? I/art: Not late-enabling -Xcheck:jni (already on) 03-09 10:26:28.033 3190-3190/com.example.jenny.funfacts W/System: ClassLoader referenced unknown path: /data/app/com.example.jenny.funfacts-2/lib/x86 03-09 10:26:28.169 3190-3190/com.example.jenny.funfacts D/AndroidRuntime: Shutting down VM 03-09 10:26:28.169 3190-3190/com.example.jenny.funfacts E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jenny.funfacts, PID: 3190 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jenny.funfacts/com.example.jenny.funfacts.FunFactsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2090) at com.example.jenny.funfacts.FunFactsActivity.<init>(FunFactsActivity.java:14) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1067) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  03-09 10:26:30.326 3190-3197/com.example.jenny.funfacts W/art: Suspending all threads took: 21.628ms 03-09 10:26:33.781 3190-3190/? I/Process: Sending signal. PID: 3190 SIG: 9

Ok, well from looking at your log cat it's, saying that you have a NullPointerException, meaning that something in your code is attempting to invoke a method on, or access a property from, a reference variable whose current value is null(meaning, it has no value assigned). Before you go any further. Go in and check your activity_fun_facts.xml and make sure ALL of the id's that you're declaring and assigning in FunFactActivity.java match up with their corresponding component id's inside of activity_fun_facts.xml.

If that doesn't end up being your problem I suggest you just download the project files, extract them to your desktop or wherever you want. Then, compare your files with his, and see if you can spot the error that way. Good luck.