androidfacebook-sharerelease-builds

Unexpected Cast to ShareButton [WrongViewCast] Error on Release Build Android Studio


I have Facebook Share button in one of my Activities of Android App built in Android Studio. On Build Type Debug, it works perfectly fine. But when changed to Release, Build Fails with the following error.

Error:(190) Error: Unexpected cast to ShareButton: layout tag was ImageView [WrongViewCast]

This is happening with FB ShareButton only.

Can anyone help?

Activity.xml

<RelativeLayout 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/activity_news"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@color/colorWhite"
    tools:context="com.*****.*****.News">

    <ImageView
        android:id="@+id/imgMainImage"
        android:layout_width="70dp"
        android:layout_height="250dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:maxWidth="70dp"
        android:scaleType="centerCrop" />

    <ImageView
        android:id="@+id/imgUserImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="15dp"
        android:foregroundGravity="center"
        app:srcCompat="@drawable/logo_app" />

    <TextView
        android:id="@+id/lblChangePasswordTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imgMainImage"
        android:layout_alignParentStart="true"
        android:background="@drawable/news_headline_tint"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:textColor="@android:color/background_light"
        android:textSize="20sp"
        android:textStyle="normal|bold"
        android:layout_alignParentEnd="true" />

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/transparent"
        android:text="Logout"
        android:onClick="logOut"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btnLogout"
        android:layout_below="@+id/imgMainImage"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:id="@+id/scrollView2">

        <TextView
            android:id="@+id/lblFullText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/btnLogout"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/imgMainImage"
            android:text="TextView" />
    </ScrollView>

    <ImageView
        android:id="@+id/imgFBShare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/lblChangePasswordTitle"
        android:layout_alignEnd="@+id/scrollView2"
        android:layout_marginBottom="12dp"
        android:onClick="shareFB"
        app:srcCompat="@drawable/com_facebook_button_icon_blue" />

</RelativeLayout>

It is the last ImageView.


Solution

  • Change your XML from:

    <ImageView
            android:id="@+id/imgFBShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/lblChangePasswordTitle"
            android:layout_alignEnd="@+id/scrollView2"
            android:layout_marginBottom="12dp"
            android:onClick="shareFB"
            app:srcCompat="@drawable/com_facebook_button_icon_blue" />
    

    to:

    <com.facebook.share.widget.ShareButton
            android:id="@+id/imgFBShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/lblChangePasswordTitle"
            android:layout_alignEnd="@+id/scrollView2"
            android:layout_marginBottom="12dp"
            android:onClick="shareFB"
            app:srcCompat="@drawable/com_facebook_button_icon_blue" />
    

    The problem you're having is that you're trying to get the functionality from the ShareButton and you're casting as such, but unfortunately in your XML you're pointing to an ImageView instead of the ShareButton class provided by Facebook.