Home > Back-end >  My app crashes when i try to move to a particular layout
My app crashes when i try to move to a particular layout

Time:07-30

My log cat :

2022-07-29 21:42:03.128 24303-24303/com.example.newcadviewerapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.newcadviewerapp, PID: 24303 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newcadviewerapp/com.example.newcadviewerapp.ShareMedia}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.Button at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3729) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:263) at android.app.ActivityThread.main(ActivityThread.java:8296) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006) Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.Button at com.example.newcadviewerapp.ShareMedia.onCreate(ShareMedia.kt:28) at android.app.Activity.performCreate(Activity.java:8153) at android.app.Activity.performCreate(Activity.java:8137) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3698) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:263)  at android.app.ActivityThread.main(ActivityThread.java:8296)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)

The layout(share_photo2.xml) for which the app gets crashed :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@ id/app_bar"
        android:background="@android:color/white"
        >
        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:id="@ id/profile_toolbar">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@ id/close_share_post_button"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:contentDescription="@string/to_decline_share_post"
                    android:src="@drawable/cross">

                </ImageView>

                <ImageView
                    android:id="@ id/share_post_buttom"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="5dp"
                    android:layout_marginEnd="10dp"
                    android:contentDescription="@string/share_the_post_into_feed"
                    android:src="@drawable/tick">

                </ImageView>

            </RelativeLayout>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/app_bar">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:id="@ id/image_preview"
            android:src="@drawable/add_image_icon"
            android:scaleType="centerCrop"
            android:layout_marginTop="10dp"
            android:contentDescription="@string/preveiw_of_the_image_to_be_posted">

        </ImageView>

        <EditText
            android:id="@ id/post_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="5dp"
            android:layout_marginTop="10dp"
            android:hint="@string/say_something_about_the_post"
            android:inputType="text"
            android:minHeight="48dp"
            android:textColorHint="#757575"
            android:autofillHints="">

        </EditText>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <include layout="@layout/bottom_nav" />
    </RelativeLayout>
</RelativeLayout>

bottom_nav.xml :

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/layout1"
        android:layout_width="match_parent"
        android:layout_height="55dp"

        >


        <ImageButton
            android:id="@ id/add_picture_button_bottomnav"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="44dp"
            android:contentDescription="@string/add_pictures"
            app:layout_constraintEnd_toStartOf="@ id/imageButton3"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/ic_input_add" />

        <ImageButton
            android:id="@ id/search_button_bottomnav"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginTop="4dp"
            android:layout_weight="1"
            android:contentDescription="@string/search_for_recruiter_and_peers"
            app:layout_constraintEnd_toStartOf="@ id/add_picture_button_bottomnav"
            app:layout_constraintHorizontal_bias="0.4"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/ic_menu_search" />

        <ImageButton
            android:id="@ id/imageButton3"
            android:layout_width="54dp"
            android:layout_height="50dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="52dp"
            android:contentDescription="@string/profile_page"
            android:scaleType="fitCenter"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/img_1" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</merge>

ShareMedia.kt :

package com.example.newcadviewerapp

import android.app.Activity
import android.app.Instrumentation
import android.media.Image
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

import androidx.viewpager2.widget.ViewPager2
import com.google.firebase.storage.FirebaseStorage
import com.google.firebase.storage.StorageReference

import com.squareup.picasso.Picasso

class ShareMedia: AppCompatActivity() {
    lateinit var viewPager2: ViewPager2
    lateinit var storagePostPicRef : StorageReference
    private lateinit var image_preview : ImageView
    lateinit var share_post_buttom:Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.share_photo2)
        image_preview=findViewById(R.id.image_preview)
        share_post_buttom=findViewById(R.id.share_post_buttom)

        val contract=registerForActivityResult(ActivityResultContracts.GetContent()){     // We get the uri of the image
            image_preview.setImageURI(it)
        }

        storagePostPicRef=FirebaseStorage.getInstance().reference.child("Post Pictures")

        image_preview.setOnClickListener ()
        {
            contract.launch("image/*")
        }


    }

}

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.newcadviewerapp">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CAMERA"/>



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.NewCADViewerApp">
        <activity
            android:name=".LoginActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity android:name=".JoinActivity"/>
        <activity android:name=".MainActivity"/>
        <activity android:name=".RecruiterProfile"/>
        <activity android:name=".ShareMedia">

        </activity>



    </application>


</manifest>

CodePudding user response:

You've declared lateinit var share_post_buttom:Button as Button but on your XML it's an ImageView

<ImageView
                    android:id="@ id/share_post_buttom"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="5dp"
                    android:layout_marginEnd="10dp"
                    android:contentDescription="@string/share_the_post_into_feed"
                    android:src="@drawable/tick">

That's why your logcat shows:

 androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.Button
  • Related