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 (retired 2014) Getting Started with Android Adding an OnClickListener to a Button

Sage Elliott
Sage Elliott
30,003 Points

Onclick Listener Crashes app

Hello, I am having a problem. Every time i add the onclick listener code shown in the video the app crashes when I try to run it. there is of course more code now initially in the main activity file than shown in the video. Im curious if that has something to do with it? I'm also having the same issue following other android tutorials on youtube.

Does anyone know of a common problem that would be causing this?

I will add the code in a separate comment in a bit.

Sage Elliott
Sage Elliott
30,003 Points
package com.example.myfirstapp;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
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.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        // CODE I ADDED BEGINS BELOW:
        //declare view variables
        final TextView answerLabel = (TextView) findViewById(R.id.textView1);
        Button getAnswerButton = (Button) findViewById(R.id.button1);

        getAnswerButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // button was clicked
                String answer = "yes";
                answerLabel.setText(answer);

            }
        });

        //END OF CODE THAT I ADDED.
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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_main, container, false);
            return rootView;
        }
    }

}

            ```

5 Answers

Sage Elliott
Sage Elliott
30,003 Points

I have tried re-creating the file while watching the video several times before I decided I HAD to take to posting to the forum. Now when you start a new project in eclipse or any of the IDE it seems that you have to do most the XML editing in "fragment_main" rather than the "activity_main.xml" shown in the video, but everything thing works fine until I add in the onclick listener.

Thank you for your time viewing my question and I hope you can help a java me out, so I can pay it forward in the future!

yeah I have the same problem, although it works when I call things that are inside my class but the problem occurs when I try to call stuff that are from other classes. However, when I call those same outside class methods without using the onclicklistener they still work...

Sage Elliott
Sage Elliott
30,003 Points

Did you see my post above? It has a link to what worked for me.

yea, i know that'd work, it's just when I call methods from other classes using onClick(View v) or button.setOnClickListener(new OnClickListener) it still crashes