This series is dedicated to the little things in programming that make you want to bang your head against the wall. Today’s post is about: confusing error messages.
The code
I added a navigation icon to an app bar using Jetpack Compose. Initially, the icon was a Material one. The code looked like this:
navigationIcon = {
Icon(
imageVector = Icons.Filled.ShoppingCart,
contentDescription = null
)
}
Everything worked perfectly. I had a nice little shopping cart icon next to the app title. Then, I wanted to use a custom icon with the Android Studio Image Asset tool. I created the icon and plugged it into the code.
navigationIcon = {
Icon(
imageVector = ImageVector.vectorResource(R.drawable.ic_logo),
contentDescription = null
)
}
However, this time, the Compose Preview wouldn’t render. 😔
The error message
org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT PNG
IHDR ...@6:155 in java.io.InputStreamReader@ 25a0b497) at org.kxml2.io.KXmlParser.exception( Unknown Source) at org.kxml2.io.KXmlParser.error( Unknown Source) at
org.kxml2.io.KXmlParser.pushEntity( Unknown Source)
...
My eyes jumped straight to the PNG
in the error message. And down the rabbit hole I went.
- Was the PNG corrupted?
- Did it have too many colors?
- Was the size too large?
- And on and on…
Nothing worked. I restarted Android Studio, cleared the caches, still no dice.
Two days later
I stared at the code with a cup of coffee in my hands. And then I thought XmlPullParserException
? This is a PNG. And so, I switched the imageVector
property to a painter
property.
navigationIcon = { Icon( painter = painterResource(R.drawable.ic_logo), contentDescription = null ) }
And yeah, it worked!
The error message was confusing and unhelpful. If it said, “This is not a vector image, it’s a PNG.” I would have spotted my error right away and moved on.
Just one of those little things. fin