javaandroidxmlcrashandroid-runtime

Application keeps crashing when clicking register button and not showing preview in design mode?


I am having serious difficulties trying to figure this out. I have a landing page in my application and when i go to click the register button the application will crash and i will get this error;

             --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ie.app.barbershop, PID: 3181
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.app.barbershop/ie.app.barbershop.Landing}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:807)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                      at ie.app.barbershop.Landing.onCreate(Landing.java:23)
                      at android.app.Activity.performCreate(Activity.java:5990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
I/Process: Sending signal. PID: 3181 SIG: 9
Application terminated.

This is my code for Landing.java, i know its something to do with my setContent but i cant figure it out

package ie.app.barbershop;

import android.content.Intent;

import android.os.Bundle;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.ScrollView;

import android.widget.TextView;

import java.util.List;


public class Landing extends AppCompatActivity{

    public Button buttonProducts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_landing);

        countRecords();

        buttonProducts = findViewById(R.id.buttonProducts);

        Button buttonCreateAppointment = findViewById(R.id.buttonCreateAppointment);
        buttonCreateAppointment.setOnClickListener(new OnClickListenerCreateAppointment());

        buttonProducts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Landing.this, Products.class));
            }
        });


    }

    public void readRecords() {

        LinearLayout linearLayoutRecords = findViewById(R.id.linearLayoutRecords);
        linearLayoutRecords.removeAllViews();

        List<ObjectAppointment> appointments = new TableControllerAppointments(this).read();

        if (appointments.size() > 0) {

            for (ObjectAppointment obj : appointments) {


                String fullName = obj.fullName;
                int contactNumber = obj.contactNumber;

                String textViewContents = fullName + " - " + contactNumber;

                TextView textViewAppointmentItem= new TextView(this);
                textViewAppointmentItem.setPadding(0, 10, 0, 10);
                textViewAppointmentItem.setText(textViewContents);
                textViewAppointmentItem.setTag(Integer.toString(contactNumber));

                linearLayoutRecords.addView(textViewAppointmentItem);
            }

        }

        else {

            TextView locationItem = new TextView(this);
            locationItem.setPadding(8, 8, 8, 8);
            locationItem.setText("No records yet.");

            linearLayoutRecords.addView(locationItem);
        }

    }

    public void countRecords(){

        int recordCount = new TableControllerAppointments(this).count();

        TextView textViewRecordCount = findViewById(R.id.textViewRecordCount);
        textViewRecordCount.setText(recordCount + " records found.");

    }
}

Also when i try to look at my application in design view i am getting this error;

java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:864)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:866)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:325)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:547)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:681)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Here is my XML file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgroundbarbershop">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="28dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/galleryImage"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_camera_alt_black_36dp" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="149dp"
        android:layout_height="144dp"
        android:layout_marginEnd="36dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/gallery"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_shopping_cart_black_36dp" />

    <ImageView
        android:id="@+id/imageView11"
        android:layout_width="149dp"
        android:layout_height="144dp"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:contentDescription="@string/appointmentPicture"
        app:layout_constraintBottom_toTopOf="@+id/buttonCreateAppointment"
        app:layout_constraintEnd_toStartOf="@+id/imageView12"
        app:layout_constraintHorizontal_bias="0.571"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/book" />

    <Button
        android:id="@+id/buttonCreateAppointment"
        android:layout_width="146dp"
        android:layout_height="54dp"
        android:layout_marginBottom="76dp"
        android:layout_marginStart="28dp"
        android:text="@string/buttonBookAppointment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/buttonProducts"
        android:layout_width="146dp"
        android:layout_height="38dp"
        android:layout_marginEnd="36dp"
        android:layout_marginTop="8dp"
        android:text="@string/buttonProducts"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

    <Button
        android:id="@+id/buttonGallery"
        android:layout_width="145dp"
        android:layout_height="37dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/buttonGallery"
        app:layout_constraintBottom_toTopOf="@+id/imageView11"
        app:layout_constraintEnd_toStartOf="@+id/buttonProducts"
        app:layout_constraintHorizontal_bias="0.487"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/button4"
        android:layout_width="140dp"
        android:layout_height="55dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/contactUsButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.314"
        app:layout_constraintStart_toEndOf="@+id/buttonCreateAppointment"
        app:layout_constraintTop_toBottomOf="@+id/imageView12"
        app:layout_constraintVertical_bias="0.157" />

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="wrap_content"
        android:layout_height="144dp"
        android:layout_marginEnd="40dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/contact"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonProducts"
        app:srcCompat="@drawable/ic_contact_phone_black_36dp" />


    <TextView
        android:id="@+id/textViewRecordCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:padding="1dp"
        android:text="@string/textViewRecordCount"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.182"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonCreateAppointment"
        app:layout_constraintVertical_bias="0.146" >

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

        </LinearLayout>

</TextView>
</android.support.constraint.ConstraintLayout>

Solution

  • You have a LinearLayout inside your TextView:

    <TextView
        android:id="@+id/textViewRecordCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:padding="1dp"
        android:text="@string/textViewRecordCount"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.182"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonCreateAppointment"
        app:layout_constraintVertical_bias="0.146" >
    
        <LinearLayout
            android:id="@+id/linearLayoutRecords"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
        </LinearLayout>
    
    </TextView>
    

    If you just removed your Linear Layout should work fine. TextView is not a View Group, so you can't have things inside it. That's why you're getting:

    AppCompatTextView cannot be cast to android.view.ViewGroup