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 Making a Button Do Something

6 Answers

JT Keller
JT Keller
12,731 Points

Did you check the logs or insert any println statements with console output that could possibly help you debug the issue?

Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 775 Points

This is what the log says:

Unable to start activity ComponentInfo{com.reisventures.funfacts/com.reisventures.funfacts.FunFactsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

All my code is the same as in the video! :/

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

From your error statement it looks like a button has not been initialized correctly. Can you post your code?

Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 775 Points

Hey Jon Thanks a lot in advance!

Code:

package com.reisventures.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);

    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) {
            String fact = "Ostriches can run faster than horses.";
            factLabel.setText(fact);
        }
    };

    showFactButton.setOnClickListener(listener);
Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Nothing jumps out to me yet. Could you try posting your activity_fun_facts.xml file?

Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 775 Points

Hey Jon,

You tipped me! I went to the xml file and saw that the id was different for the button! I didn't know capital letters metter! ;)

Anyway, I'm really struggling to understand why we need to use these 2 lines of code. I know a bit of VBA, and it is more straight forward!

I get the part of the click listener, basically I'm creating and action. I don't understand why we need the following part of the code and why we relate it to the id's we've created for the layouts:

Code:

final TextView factLabel = (TextView) findViewById(R.id.factTextView);

Button showFactButton = (Button) findViewById(R.id.showFactButton);

Thanks in advance.

Duarte

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Before those two lines, the "button" or "textview" only exists in your layout file (the xml). In order to interact with them, we need to make a reference to it in code. That's what those two lines are doing.

If we had two buttons in our application, we would need to store both of them in variables/initialize them:

Button showFactButton = (Button) findViewById(R.id.showFactButton);

Button showSomethingElseButton = (Button) findViewById(R.id.showDateButton);

Say we didn't have those lines and instead had:

Button button;

How would our program know which button that corresponds to in our layout file? Is it a showFactButton? Or does it show the date?

Let me know if there is something I can explain further. It does take a little time getting used to.

Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 775 Points

Thanks a lot! It is much more clear now!

The process of learning this things is really frustrating! While I (now) understand the code and what it does, I would never be able to replicate it by myself! :)

Cheers,

Duarte