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

alex lewis
alex lewis
1,324 Points

Unfortunately, Fun Facts has stopped. (using my phone as an emulator.)

So, i'm not sure why the app isn't running. I have been using my phone as an emulator and it worked before we added this chunk of code to it. Now it doesn't run. I feel like i've typed something wrong. I also wanted to know why we are treating the first TextView as a button when all it does is say "Did You Know". Why is that a button?

package com.training.alex.funfacts;

import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

import org.w3c.dom.Text;

public class FunFacts extends ActionBarActivity {

@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)
        {
            //anything in here will happen anytime the object is clicked
            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);
}

} HERE IS THE OTHER CODE

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".FunFacts" android:background="#ff51b46d">

<TextView android:text="Did You Know?" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/showFactbutton"
    android:textSize="24sp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="#80ffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ants stretch when they wake up in the morning."
    android:id="@+id/factTextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="24sp"
    android:textColor="@android:color/white" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show Another Fun Fact"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="35dp"
    android:background="@android:color/white" />

</RelativeLayout>

1 Answer

alex lewis
alex lewis
1,324 Points

nevermind, i fixed it. i had the wrong button id on the .xml It shows properly now.

May Lye
May Lye
10,505 Points

Hi Alex...I got the exactly same problem as yours...and I also using my phone as emulator...may I know how u fixed it? I just couldn't fix it....