I am trying to add a float to my dimens.xml
file.
I was reading the following SO answer. When I tried the solution, I got the exception described in the comments. I am trying to figure out why that exception is thrown.
For completeness here is the XML:
<item name="zoom_level" format="float" type="dimen">15.0</item>
Here is the code that blows up:
final float zoom = this.getResources().getDimension(R.dimen.zoom_level);
I jumped into the Android source, 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 whatever reason value.type != TypedValue.TYPE_DIMENSION
. I do not have my Android source completely set up so I cannot easily add a Log.w("YARIAN", "value type is " + value.type)'
statement in there.
I then jumped into getValue
and the chain of calls seems to be:
Resources.getValue -> AssetManager.getResourceValue -> AssetManager.loadResourceValue
loadResourceValue
is a native method and here is where my digging falls apart.
Anybody know what the best way to understand what's going is?
I also noticed that Resources
has a TypedValue.TYPE_FLOAT
and TypedValue.TYPE_DIMENSION
. But in XML, I cannot write type="float"
.
The work around described in the comments is to use type=string
and then use Float.parse
to get the float. Is this necessary? Why or why not?
NotFoundException. Stay organized with collections Save and categorize content based on your preferences. This exception is thrown by the resource APIs when a requested resource can not be found.
For each type of resource, there is an R subclass (for example, R. drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.
You can use catch(Resources. NotFoundException e) . The $ in your stack trace just means NotFoundException is a nested class in Resources . You could import the fully qualified class for NotFoundException - that is, android.
I know it's a late answer but you should use TypedValue#getFloat() instead of parsing the String to a float like you suggested.
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 the type
if you prefer, this only corresponds to the resource class in R
.
I just ran into this problem too, and though the error message isn't too helpful, I realized my problem was that I was putting just a float value in my resource file and didn't specify a measurement. Switching 15.0 to 15.0dp for instance would avoid the problem and allow you to still use a regular dimension resource.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With