Monday, November 14, 2011

Resources NotFoundException in Android

Sometimes (ok frequently) I do stupid things.  Here was my offending line:

list.setAdapter(new ArrayAdapter<Foo>(this, android.R.id.text1, foos));

Which was met at runtime by this stack trace:

android.content.res.Resources$NotFoundException: File  from xml type layout resource ID #0x1020014
at android.content.res.Resources.loadXmlResourceParser(Resources.java:1916)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:1871) 
at android.content.res.Resources.getLayout(Resources.java:731)
at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
   

Not sure what I was smokin when I wrote the code.  Was in a hurry.  Problem is with the android.R.id.text1.  The parameter is supposed to be a layout, not an id.

Correct code is something like:


list.setAdapter(
   new ArrayAdapter<Foo>(this, android.R.layout.simple_list_item_1, foos));

Or whatever layout you like.  

This problem was a little obscure because I was just trying to do something very quickly and didn't have any exception handling in place.  Additionally, none of my code appeared in the stack trace.

Since it's not obvious from the stack trace what the problem is, I've posted it here.  I tend to do the same stupid things at approximately 6-month intervals :-)

 




No comments:

Post a Comment