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

Josh Myers
Josh Myers
3,203 Points

Error running Fun Facts app

When I ran the fun facts app for the first time it said "Unfortunately Fun Fact has stopped" I looked through the error log, but can't make sense of it. This is what it gave me:

1-03 00:28:57.430 1000-1000/com.example.josh.funfact E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.josh.funfact, PID: 1000 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.josh.funfact/com.example.josh.funfact.FunFactsActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 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:5017) 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:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.josh.funfact.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:2159)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)             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:5017)             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:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)             at dalvik.system.NativeStart.main(Native Method)

I just pasted the red lines in the error log...

Any suggestions?

2 Answers

Jaroslav Vankat
Jaroslav Vankat
12,054 Points

Seems to be a NullPointerException, try posting the whole code of your FunFactsActivity.

Josh Myers
Josh Myers
3,203 Points

Ok, here it is. Thanks!

package com.example.josh.funfact;

import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; 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 variables and assign them 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 a new fact
            String fact = "Ostriches can run faster than horses.";
            factLabel.setText(fact);
        }
    };
   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);
}

}

Jaroslav Vankat
Jaroslav Vankat
12,054 Points

Seems to be ok, try declaring the views as member variables and assigning the views to them:

public class FunFactsActivity extends Activity {

Button mShowFactButton;
TextView mFactLabel;

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

    mFactLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
   /** and so on */
}

The problem might be setting the mFactLabel as final, but don't no right now and no time for trying.