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 trialKristian Terziev
28,449 Points"Unfortunately, Fun Facts has stopped." Error message after I included the code in the "Adding the onClick() Method"
Hello :) I am currently at the "Adding the onClick() Method" video in the "Build a Simple Android App" course and I am facing an error that I cannot seem to understand.
I am using Genymotion's emulator (since I had a lot of issues with the Android Studio's one). The device is Google Nexus 7 - 4.4.4 - API 19 - 800x1280. I tested the basic things we did in the begining (showing just one fact in a white window etc.) and they all worked perfectly.
But now, after I added some code in the "FunFacts.java" file, the app fails to start. In the emulator it prints an error saying "Unfortunately, Fun Facts has stopped."
I though it might be something I did wrong through the steps so I did it all again from scratch. Same thing. When I check the "Messages" tab it says "0 errors, 0 warnings"
Any ideas?
7 Answers
Stone Preston
42,016 Pointsyeah try removing the if statement about the saved instance state. im not sure why thats there.
public class FunFacts extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
final TextView factLabel = (TextView) findViewById(R.id.textView);
Button showFactButton = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = "Another random fact appeared out of nowhere !!!";
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
Stone Preston
42,016 Pointsclick the android tab and see if there is anything in the log there.
Kristian Terziev
28,449 PointsLots of red lines :C. Don't know exactl which one is needed so here they are:
10-29 20:55:52.530 1214-1214/com.example.nick.funfacts D/dalvikvm﹕ Late-enabling CheckJNI
10-29 20:55:52.550 1214-1220/com.example.nick.funfacts D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
10-29 20:55:52.554 1214-1214/com.example.nick.funfacts D/AndroidRuntime﹕ Shutting down VM
10-29 20:55:52.554 1214-1214/com.example.nick.funfacts W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4ca2b20)
10-29 20:55:52.558 1214-1214/com.example.nick.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nick.funfacts, PID: 1214
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nick.funfacts/com.example.nick.funfacts.FunFacts}: 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.example.nick.funfacts.FunFacts.onCreate(FunFacts.java:39)
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)
10-29 21:11:36.010 1277-1277/com.example.nick.funfacts D/dalvikvm﹕ Late-enabling CheckJNI
10-29 21:11:36.054 1277-1277/com.example.nick.funfacts D/AndroidRuntime﹕ Shutting down VM
10-29 21:11:36.054 1277-1277/com.example.nick.funfacts W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4ca2b20)
10-29 21:11:36.066 1277-1277/com.example.nick.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nick.funfacts, PID: 1277
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nick.funfacts/com.example.nick.funfacts.FunFacts}: 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.example.nick.funfacts.FunFacts.onCreate(FunFacts.java:39)
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)
Stone Preston
42,016 Pointsok yeah you have a null pointer exception being thrown. can you post the class that contains the onClick listener please?
Kristian Terziev
28,449 Pointspublic class FunFacts extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_facts);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
final TextView factLabel = (TextView) findViewById(R.id.textView);
Button showFactButton = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = "Another random fact appeared out of nowhere !!!";
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
I have an "if" statement, unlike the video I'm watching. Has some check for null maybe that's the thing!?
Kristian Terziev
28,449 PointsYe the thing is, I tried that already. But I still got the same problem. Here's the "Android" tab with the new code:
10-29 21:31:08.278 1600-1600/com.example.nick.funfacts D/dalvikvm﹕ Late-enabling CheckJNI
10-29 21:31:08.334 1600-1600/com.example.nick.funfacts D/AndroidRuntime﹕ Shutting down VM
10-29 21:31:08.334 1600-1600/com.example.nick.funfacts W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4ca2b20)
10-29 21:31:08.334 1600-1600/com.example.nick.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nick.funfacts, PID: 1600
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nick.funfacts/com.example.nick.funfacts.FunFacts}: 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.example.nick.funfacts.FunFacts.onCreate(FunFacts.java:34)
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)
Kristian Terziev
28,449 PointsOk I fixed it. Turns out I was clicking on "Blank Activity with Fragment" both times I tried. Rewatched the video and tried it with "Blank Activity" and it all worked out. Sorry for my mistake and thank you for your time :)