Search code examples
androidandroid-appcompatandroid-framelayoutandroid-cardview

Card View (Appcompat) causes app to crash


I am trying to use the Android Quiz sample but and I want to put all of the contents into a card view. I used the example of a card view (Which works completely) from the Android Navigation Drawer Sample. When running my app, it crashes. Yet no bugs are shown in the code. What is causing this? Here is my code:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<ScrollView 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.android.support.wearable.quiz.MainActivity"
        tools:ignore="MergeRootFrame">

    <LinearLayout android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/read_quiz_from_file_button"
            android:text="@string/read_from_file_button"
            android:onClick="readQuizFromFile"
            android:layout_gravity="center"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="@string/edit_question"
                android:id="@+id/textView2" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/question_text" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/choices_radio_group">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:text="@string/edit_choice_a"
                    android:id="@+id/choice_a_radio"
                    android:checked="true" />
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:text="@string/edit_choice_b"
                    android:id="@+id/choice_b_radio"
                    android:checked="false" />
                <RadioButton
                        android:layout_width="wrap_content"
                    android:layout_height="50dp"
                        android:text="@string/edit_choice_c"
                        android:id="@+id/choice_c_radio" />
                <RadioButton
                        android:layout_width="wrap_content"
                    android:layout_height="50dp"
                        android:text="@string/edit_choice_d"
                        android:id="@+id/choice_d_radio" />
            </RadioGroup>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:id="@+id/choice_a_text" />
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:id="@+id/choice_b_text" />
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:id="@+id/choice_c_text" />
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:id="@+id/choice_d_text" />
              </LinearLayout>


        </LinearLayout>

        <Button android:id="@+id/add_question"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/add_question"
            android:onClick="addQuestion"
            android:layout_gravity="center">
        </Button>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/quiz_status"
            android:id="@+id/quiz_status"
            android:paddingTop="30dp"
            android:paddingBottom="10dp"
            android:visibility="invisible"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/questions_container"
            android:orientation="vertical"
            android:paddingBottom="10dp">
         </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/quiz_buttons"
            android:layout_gravity="center"
            android:visibility="invisible">
            <Button android:id="@+id/reset_quiz_button"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/reset_quiz"
                android:onClick="resetQuiz">
            </Button>
            <Button android:id="@+id/new_quiz_button"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/new_quiz"
                android:onClick="newQuiz">
            </Button>
        </LinearLayout>


    </LinearLayout>

</ScrollView>
</android.support.v7.widget.CardView>
</FrameLayout>

For reference, here is the code of the Navigation Drawer Sample's card view:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- The CardView needs to be wrapped to ensure spacing is applied correctly. -->

    <android.support.v7.widget.CardView
        style="@style/Widget.SampleDashboard.Card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            style="@style/Widget.SampleDashboard.Item"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@android:id/text1"
                style="@style/Widget.SampleDashboard.Item.Title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello world" />

            <TextView
                android:id="@android:id/text2"
                style="@style/Widget.SampleDashboard.Item.Description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </android.support.v7.widget.CardView>

</FrameLayout>

Here is the code that was posted on the blog for this:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- Your card content -->

</android.support.v7.widget.CardView>

Any help would be appreciated. I am very new to android development.


Solution

  • Just adding an answer if you'd like to accept it. The issue was a ClassNotFoundException because the gradle dependency for CardView was not added.