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 Initializing a Button

why am i getting a compiler error

its failing to compile

FunFactsActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FunFactsActivity extends Activity {

    @Override
    public 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
        TextView factLable= (TextView) findViewById(R.id.factTextview);
        Button showFactButton;

    }
}
Nitin George Cherian
Nitin George Cherian
4,410 Points

Could you please post the compiler error as well as the activity_fun_facts.xml file ?

<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:id="@+id/showFactButton" android:background="#51b46d">

<TextView android:text="Did you know?" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:textColor="#80ffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ants stretch when the wake up in the morning"
    android:id="@+id/factTextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="25sp"
    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/showFactButton"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white" />

</RelativeLayout>

/*09-23 15:00:30.467 32093-32093/biz.appfarm.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: biz.appfarm.funfacts, PID: 32093 java.lang.RuntimeException: Unable to start activity ComponentInfo{biz.appfarm.funfacts/biz.appfarm.funfacts.FunFacts}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 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:5001) 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:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button at biz.appfarm.funfacts.FunFacts.onCreate(FunFacts.java:21) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)             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:5001)             at java.lang.reflect.Method.invokeNative(Native Method) */

1 Answer

Hi Tasokwa,

I think the problem is this line of code:

TextView factLable= (TextView) findViewById(R.id.factTextview);

If you didn't use auto complete follow the below.

factTextview should be: factTextView, I think (capital V for view). And factLable should be: factLabel, I think ("el" not "le" for label)

TextView factLable= (TextView) findViewById(R.id.factTextview);

Fire back if this doesn't work (or help).