Resources $ NotFoundException: The resource identifier is invalid. What for?

I am trying to add float to my dimens.xml file.

I read the following SO answer . When I tried the solution, I got the exception described in the comments. I am trying to understand why this exception is thrown.

For completeness, XML is provided here:

 <item name="zoom_level" format="float" type="dimen">15.0</item> 

Here is the code that explodes:

 final float zoom = this.getResources().getDimension(R.dimen.zoom_level); 

I moved to the Android source code and here is the method definition for getDimension:

 public float getDimension(int id) throws NotFoundException { synchronized (mTmpValue) { TypedValue value = mTmpValue; getValue(id, value, true); if (value.type == TypedValue.TYPE_DIMENSION) { return TypedValue.complexToDimension(value.data, mMetrics); } throw new NotFoundException( "Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid"); } } 

So for some reason value.type != TypedValue.TYPE_DIMENSION . I don’t have a fully configured source for Android, so I can’t easily add the Log.w("YARIAN", "value type is " + value.type)' instruction Log.w("YARIAN", "value type is " + value.type)' .

Then I jumped into getValue , and the call chain looks like this:

 Resources.getValue -> AssetManager.getResourceValue -> AssetManager.loadResourceValue 

loadResourceValue is a native method, and this is where my digging falls apart.

Does anyone know the best way to understand what is happening?


I also noticed that Resources has a TypedValue.TYPE_FLOAT and TypedValue.TYPE_DIMENSION . But in XML I cannot write type="float" .

The work described in the comments is to use type=string , and then use Float.parse to get a float. It's necessary? Why or why not?

+4
source share
2 answers

I know this is a late answer, but you should use TypedValue # getFloat () instead of casting the String into the float as you intended.

XML:

  <item name="float_resource" format="float" type="raw">5.0</item> 

Java:

 TypedValue out = new TypedValue(); context.getResources().getValue(R.raw.float_resource, out, true); float floatResource = out.getFloat(); 

You can put fraction , raw or string as type , if you want, this corresponds to the resource class in R

+13
source

I also ran into this problem, and although the error message is not very useful, I realized that the problem is that I put only a floating value in the resource file and did not indicate the dimension. For example, switching from 15.0 to 15.0dp will avoid the problem and allow you to use a regular measurement resource.

0
source

All Articles