Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Basic Android Programming Adding the onClick() Method

Timothy Williams
Timothy Williams
1,536 Points

Lesson correct? I am stuck. showFactButton.setOnClickListener(listener); causes crash

Let me say this: I like the IDEA of Treehouse. I like the 3 JAVA lessons on this Android Track. But they were NOT enough before jumping into this course which suddenly changes from changing color in XML to this last lesson. The code does not match the CURRENT Android Studio. I taught myself to use the debugger, step through and find the crash point (that should be ADDED to this track earlier). If I comment out //showFactButton.setOnClickListener(listener); then it runs as before the lesson, else it crashes the app. Here is the code copy/paste.

package com.shootmyphoto.funfacts;

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.os.Build;
import android.widget.Button;
import android.widget.TextView;


public class FunFactsActivity extends ActionBarActivity {

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


        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        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 v) {
                //button clicked update fact label
                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.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();

        //noinspection SimplifiableIfStatement
        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_fun_facts, container, false);
            return rootView;
        }
    }
}
Timothy Williams
Timothy Williams
1,536 Points

Let me answer my own question. I do not know what a FRAGMENT is... but my XML layout had the relative layout in a fragment and there was auto-generated code referring to it. I manually moved the XML to the activity_fun_facts.xml, did a Safe Delete on the fragment_activity_fun_facts.xml after cleaning up the FunFactsActivity.java references to the fragments. I do not know how this project got into that territory, but it caused me 3-4 hours of research and some trial/error. Hope this helps someone!

1 Answer

Nicolas Hampton
Nicolas Hampton
44,626 Points

When you're following along with this course, be very careful to use autofill when the instructor uses autofill, because he's not telling us about certain dependencies that android studio automatically adds when autofill is used. I had similar problems until I caught that.