androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout

I am getting such an error while compiling the application.
When I make a LinearLayout, the error disappears, but when I use a ConstraitLayout, the error appears.

enter image description here

Java Class

        LinearLayout linearLayout = findViewById(R.id.parent); /*Your root view to be part of screenshot*/
        linearLayout.buildDrawingCache();
        Bitmap bmp = linearLayout.getDrawingCache();
        // Store image to default external storage directory
        Uri bmpUri = null;
        File file;
        try {
            File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/" + getString(R.string.app_name) + "/");
            dir.mkdirs();
            String fileName = "/" + System.currentTimeMillis() + "share_image.png";

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                file = new File(dir, fileName);
            } else {
                file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            }

            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }

Xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:layoutDirection="ltr"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/relativeLayout7"
        android:layout_width="1280dp"
        android:layout_height="55dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:background="@color/colorPrimary"
        android:gravity="center_horizontal|center_vertical"
        android:layoutDirection="rtl"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/level_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_marginTop="2dp"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:ignore="RtlCompat" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/level_number"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"
            android:layout_toStartOf="@+id/score"
            android:layout_toLeftOf="@+id/score"
            android:layout_toEndOf="@+id/questionNumber"
            android:layout_toRightOf="@+id/questionNumber"
            android:gravity="center_vertical|center_horizontal"
            android:max="10"
            android:progress="10"
            android:progressDrawable="@drawable/circular_progress_bar2" />

        <TextView
            android:id="@+id/questionNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_marginStart="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="1dp"
            android:background="@drawable/item_background"
            android:gravity="center_horizontal|center_vertical"
            android:paddingLeft="20dp"
            android:paddingTop="10dp"
            android:paddingRight="20dp"
            android:paddingBottom="10dp"
            android:text="0 / 0"
            android:textColor="@color/colorAccent"
            android:textSize="20sp"
            tools:ignore="RtlCompat" />


        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_marginLeft="1dp"
            android:layout_marginEnd="1dp"
            android:background="@drawable/item_background"
            android:drawableEnd="@drawable/score"
            android:drawablePadding="15dp"
            android:gravity="center_horizontal|center_vertical"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingRight="30dp"
            android:paddingBottom="10dp"
            android:text="0"
            android:textColor="@color/colorAccent"
            android:textSize="20sp"
            tools:ignore="RtlCompat" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/relativeLayout7"
        tools:layout_editor_absoluteX="29dp">


        <TextView
            android:id="@+id/questionText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:paddingLeft="10dp"
            android:paddingTop="5dp"
            android:paddingRight="10dp"
            android:paddingBottom="5dp"
            android:scrollbars="vertical"
            android:textColor="@color/textColor2"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="631dp"
        android:layout_height="345dp"
        android:layout_gravity="center_vertical|center_horizontal"

        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/done"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
        app:layout_constraintVertical_bias="0.104">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/answerGridview"
            android:layout_width="406dp"
            android:layout_height="123dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:columnWidth="40dp"
            android:gravity="center"
            android:horizontalSpacing="4dp"
            android:padding="4dp"
            android:scrollbars="none"
            android:verticalSpacing="4dp" />



            <Button
                android:id="@+id/reveal"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@drawable/border_background"
                android:drawableLeft="@drawable/reveal"
                android:drawablePadding="-5dp"
                android:padding="5dp"
                android:text="@string/show_answer"
                android:textAllCaps="false"
                android:textColor="@color/colorAccent" />

            <ImageButton
                android:id="@+id/reset"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="2dp"
                android:background="@drawable/reset_skip_background"
                android:padding="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/reset" />

            <ImageButton
                android:id="@+id/skip"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_gravity="center_horizontal"
                android:layout_margin="2dp"
                android:background="@drawable/reset_skip_background"
                android:padding="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/skip" />


        <Button
            android:id="@+id/ask"
            android:layout_width="139dp"
            android:layout_height="615dp"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:background="@drawable/border_background"
            android:drawableRight="@drawable/friends"
            android:padding="5dp"
            android:text="@string/askfriends"
            android:textAllCaps="false"
            android:textColor="@color/colorAccent" />



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/choicesGridview"
                android:layout_width="405dp"
                android:layout_height="127dp"
                android:layout_gravity="center"
                android:layout_margin="5dp"
                android:columnWidth="40dp"
                android:gravity="center"
                android:padding="4dp"
                android:scrollbars="none"

                />

        </LinearLayout>
    </LinearLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="1268dp"
        android:layout_height="91dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="4dp"
        app:adSize="SMART_BANNER"
        app:adUnitId="@string/admob_banner_unit_id"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/done"
        android:layout_width="1276dp"
        android:layout_height="76dp"
        android:background="@drawable/pressed_no_corners"
        android:text="@string/done"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageButton
        android:id="@+id/voiceImage"
        android:layout_width="120dp"
        android:layout_height="66dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"

        android:alpha="0.6"
        android:background="@drawable/border_background"

        android:padding="10dp"
        android:scaleType="centerInside"
        android:src="@drawable/ic_speaker"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@+id/questionImage"
        app:layout_constraintEnd_toStartOf="@+id/linearLayout4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/questionImage"
        android:layout_width="500dp"
        android:layout_height="250dp"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:layout_marginTop="45dp"
        android:layout_marginEnd="139dp"
        android:layout_marginBottom="277dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal"
        android:maxHeight="250dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toTopOf="@+id/done"
        app:layout_constraintEnd_toStartOf="@+id/linearLayout4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize50Percent" />

</androidx.constraintlayout.widget.ConstraintLayout>

I get this error when I try to convert LinearLayout ConstraintLayout.
I changed the Java Class to ConstraintLayout, still getting the same error