This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

android.widget.ShareActionProvider cannot be cast to androidx.core.view.ActionProvider ?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have activity with following import and code:



In menu resource xml file, I'm trying to define share action provider:



As you can see I'm using the same import package in menu resource file and in the activity (android.widget.ShareActionProvider).  But for some reason I always get ClassCastException:



Im following Head First Android Development, but this books is from 2015. Some things must have certainly changed since. Or am I making some stupid import mistake?
 
Saloon Keeper
Posts: 6211
157
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The AndroidX library wasn't around in 2015. One thing I learned about AndroidX is that it can be problematic to mix its classes with non-AndroidX classes. Is there an AndroidX version of ShareActionProvider that you could use instead?
 
Mike Gosling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking on the docs and couldn't find anything. They are also using same import or

but AndroidStudio reports an error on v7. It marks it red.

So frustrating...
 
Tim Moores
Saloon Keeper
Posts: 6211
157
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turns out AndroidX actually does have one. It's called androidx.appcompat.widget.ShareActionProvider, and since it extends androidx.core.view.ActionProvider, the error you're seeing wouldn't happen with it.

Replace the class name in both Java code and layout file, and see what happens.
 
Mike Gosling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim for helping.

At the moment this is what I have in menu_resource xml file (the part that is relevant):



And this is in activity code:



Line 14 is underlined with red, saying: Inconvertible types: cannot cast android.view.ActionProvider to androidx.appcompat.widget.ShareActionProvider;

How does Android with getActionProvider() is trying to obtain ActionProvider from android.view.ActionProvider, in both cases, when I used android.widget.ShareActionProvider and now with androidx.appcompat.widget.ShareActionProvider?
 
Tim Moores
Saloon Keeper
Posts: 6211
157
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the javadocs of the androidx.appcompat.widget.ShareActionProvider class; it explains how to obtain an AndroidX-compatible ShareActionProvider rather than the one you're getting via menuItem.getActionProvider().
 
Mike Gosling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked the docs.

Now I have code like this:

Menu resource xml:



Activity:



This solved the problem.

Maybe Tim Moores can suggest some up to date book for us CS students newbies in Android

P.S. For anyone who is reading Head First Android Development:

After fixing ShareActionProvider, if you are following the book, you're most probabbly going to get NullPointerException in OrderActivity. If you have this code, like in the book:



This is going to get you NullPointer.

I just did this, reading from docs and SO, and its doing what the author wanted us to achieve:



 
Tim Moores
Saloon Keeper
Posts: 6211
157
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to hear you got that working.

I can't recommend a book. Many years ago I had Android in Action 2nd Ed., but I don't know if they came out with newer editions after that. I just followed the Android developer blog and played around with new APIs as they came out. AndroidX specifically is so new that I don't think there are books about it yet.

Head First Android likely has an errata page online somewhere - you may want to look through that as you go along. And maybe submit a link to this topic here, so other learners can benefit from it :-)
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!