9

Switching from LinearLayout to ConstraintLayout throwing this error

java.lang.ClassCastException: android.support.v7.widget.ActivityChooserView$InnerLayout cannot be cast to android.support.v7.widget.LinearLayoutCompat                                                                                                                                             

The error occurs when i try to inflate the menu in onCreateOptionsMenu() in my fragment.

My menu which I inflate :-

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">   
    <item
        android:id="@+id/action_share"
        app:showAsAction="always"
        android:orderInCategory="101"
        android:icon="@android:drawable/ic_menu_share"
        android:title="@string/action_share"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        />
</menu>

My onCreateOptionsMenu(..)

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {     
    inflater.inflate(R.menu.customerdetail, menu);      
    MenuItem menuItem = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider) 
    MenuItemCompat.getActionProvider(menuItem);         
 // If i comment out this whole part everything is fine !!
 }

Edit :- ScreenShot of the log:-

enter image description here

Update :- Adding the app:actionProviderClass="android.support.v7.widget.ShareActionProvider" item causes the error.

As suggested by ShaneCoder dropping down to 27.0.2 / changing to LinearLayout removes the error but i want to know

Why is 27.1.0 is throwing such error at runtime while inflating Class,android.support.v7.widget.ShareActionProvider ? (is it a bug in 27.1.0 )

Santanu Sur
  • 8,836
  • 7
  • 24
  • 43
  • check if you are using linear layout somewhere in the activity – Manohar Reddy Mar 08 '18 at 13:35
  • even that was my first reaction.. i removed all possible `linear Layouts` even in the fragments..!! @Redman – Santanu Sur Mar 08 '18 at 13:36
  • Somewhere in your Java code you are Cast Linear Layout with LinearLayoutCompat – Akash Mar 08 '18 at 13:42
  • Nope i havent, i checked all of those before posting the question !! @Akash – Santanu Sur Mar 08 '18 at 13:43
  • Please post full stacktrace, not only the error – OneCricketeer Mar 08 '18 at 13:58
  • Also, why do you have a constraint layout immediately inside another? – OneCricketeer Mar 08 '18 at 14:00
  • adding the full stacktrace says `Body is limited to 30000 characters; you entered 31914.` @cricket_007 and i wrapped another constraint layout in because i could not give `paddingbottom` to the immediate child of the `ScrollView` – Santanu Sur Mar 08 '18 at 14:02
  • Okay, then add the exception message plus at least 4 lines that say "Caused by", and everything between – OneCricketeer Mar 08 '18 at 14:06
  • the edited part is all i get in the logcat !! no such crash exception "caused by" exception(`except the classCastException`)...i searched but found none with this error !! @cricket_007 – Santanu Sur Mar 08 '18 at 14:10
  • @cricket_007 added the screenshot of the logcat !! – Santanu Sur Mar 08 '18 at 14:18
  • Are you sure the constraint layout is the issue? Are you sure this exception is related to your application? Can you make a [mcve], please? – OneCricketeer Mar 08 '18 at 14:18
  • okay sure ,i will check it out ..by making a short demo !! – Santanu Sur Mar 08 '18 at 14:22
  • `at ToolbarActionBar.populateOptionMenu`... Unless you're loading this layout into a Toolbar or a Share action, I don't think the ConstraintLayout is there problem. – OneCricketeer Mar 08 '18 at 14:22
  • @cricket_007 thanks !! i reached to a conclusion now and i reduced the question to as **minimal** as possible , if i comment out the whole `onCreateOptionsMenu(..)`( **menu-contains a shareActionProvider** ) inside the fragment then everything works fine. P.S this problem occurs after i switch to constraintLayout.. – Santanu Sur Mar 09 '18 at 08:06

2 Answers2

12

Not sure if this is the issue you're fighting ...

I recently upgraded my support library dependencies. With support libraries 27.1.0 I was getting this exception with proven code. I'm using 27.0.2 now. This exception is gone.

Not tested or proven, but thought it might help.

Edit

I'm not sure what is wrong with 27.1.0. I've had to back up a minor release in the past to resolve from upgrading to the latest support libraries. I guess nobody's perfect. I believe 27.0.2 is the latest release prior to 27.1.0

Edit 2

Here's a link to the issue on Googles Issue Tracker. It's marked fixed February 28, 2018. Should be released soon.

Edit 3

Confirmed fixed in 27.1.1

ShaneCoder
  • 741
  • 4
  • 11
0

I also faced same issue. It is bug. SharedActionProvider throws exception in 27.1.0 It got resolved after upgrading to 27.1.1 Hope this will help. Have a look at this link Fixed issues in 27.1.1

Sadashiv
  • 773
  • 8
  • 18