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

setContentView() fails to create the layout causing the app to crash

This is the layout file which I have set using the setContentView() in onCreate() method of Android. It is a long code use to create a Tic Tac Toe. I have used log messages, done debugging and used toast messages but the setContentView() fails to draw the layout.Thanks in advance. Here is the 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="game.tictactoe.Game">

<RelativeLayout
    android:layout_width="350dp"
    android:layout_height="340dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="52dp">

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:onClick="click"
        android:textSize="90sp"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:onClick="click"
        android:textSize="90sp"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button3"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:onClick="click"
        android:textSize="90sp"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button5"
        android:textSize="90sp"
        android:onClick="click"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button8"
        android:textSize="90sp"
        android:onClick="click"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button4"
        android:onClick="click"
        android:textSize="90sp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button6"
        android:onClick="click"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:textSize="90sp"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button7"
        android:onClick="click"
        android:textSize="90sp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="115dp"
        android:layout_height="115dp"
        android:id="@+id/button9"
        android:textSize="90sp"
        android:onClick="click"
        android:layout_alignBottom="@id/button7"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/turn"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textSize="30sp"/>

</RelativeLayout>

1 Answer

The layout height or width of one of the views was incorrect and so setContentView() could not draw the layout. I made the layout again and it is working fine.