Win a copy of Java by Comparison (eBook) this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting classcast exception at runtime  RSS feed

 
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks in advance.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does String implement Spannable?
 
chetan dhumane
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Brown wrote:Does String implement Spannable?



No
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spannable is a subclass (or rather, sub-interface) of CharSequence. That means there can be objects out there that implement the latter, but not the former. See the Android javadocs for CharSequence to see which other such classes -besides String- exist. You'll need to do an instanceof check before casting.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

chetan dhumane wrote:

Matthew Brown wrote:Does String implement Spannable?


No


Then you won't be able to cast from a String (which is what you've created) to a Spannable. You'll have to look for a way to create a Spannable object from a String. I suspect that there'll be a class that implements Spannable that takes a String as a constructor argument.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!