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

My problem/fix for adding an onclickListener.

For anyone who’s been having trouble coming into the Android Track and having their app crashing after adding an onclick listener you may be experiencing what I did, so I thought I would post what happened to me and how to fix it.

The problem was that all the new android projects open with you editing on fragmnet_main.xml instead of activity_main.xml. you will also notice that when you try to move any text field or element in activity_main.xml you cannot move it from the upper left corner.

This was caused from the container NOT being set to “Relativelayout”. You can fix this by right-clicking on container and changing. Select “Relativelayout” and redo the tutorial in activity_main.xml(instead of fragmnet_main.xml). You can also manually change it the in the code. It should look like this in your activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myfirstapp.MainActivity"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="193dp"
        android:textSize="32sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="@string/button_text" />

</RelativeLayout>

This may not be the issue you are having with your project but it was the issue I had with mine!

1 Answer

Gideon Ooi
Gideon Ooi
326 Points

Thanks! Your solution helped greatly!

I'm also experiencing a problem with my app crashing the AVD in spite of using the provided code. It was solved once I changed the AVD settings to having 200mb of storage. So I postulate that part of the problem could also be due an underpowered AVD.