Issue related to using android attribute (attr) defined in another package aar
Suppose it attr
is defined in package aar
( com.pack1
) as
<attr name="attr0" format="string" />
and in another package (say com.pack2
that has a dependency on com.pack1
), I define styleable
as
<attr name="attr2" format="integer" />
<declare-styleable name="view2">
<attr name="attr1" format="string" />
<attr name="attr2" />
</declare-styleable>
and I have a custom view in com.pack2
like
public class View2 extends LinearLayout
{
public View2(Context context, AttributeSet attributeSet)
{
super(context, attributeSet);
View.inflate(context, R.layout.view2, this);
try
{
TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.view2);
{
if (typedArray.hasValue(R.styleable.view2_attr1))
{
String t = typedArray.getString(R.styleable.view2_attr1);
logger.debug("attr1 -> has value -> {}", t);
}
else
logger.debug("attr1 -> no value");
if (typedArray.hasValue(R.styleable.view2_attr2))
{
int t = typedArray.getInteger(R.styleable.view2_attr2, 0);
logger.debug("attr2 -> has value -> {}", t);
}
else
logger.debug("attr2 -> no value");
}
typedArray.recycle();
}
catch (Throwable t)
{
logger.debug(t.toString());
}
}
}
And I add custom view to layout as
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.pack2.View2
android:layout_width="fill_parent"
android:layout_height="wrap_content"
custom:attr1="this is a test"
custom:attr2="1000"
/>
</LinearLayout>
Until then, when I run the application everything is working fine and the log says that
attr1 -> has value -> this is a test
attr2 -> has value -> 1000
but as soon as I change styleable
how
<attr name="attr2" format="integer" />
<declare-styleable name="view2">
<attr name="attr0" />
<attr name="attr1" format="string" />
<attr name="attr2" />
</declare-styleable>
and run the application. I am getting wrong results. The registrar informs that
attr1 -> no value
and an exception happened
java.lang.UnsupportedOperationException: Can't convert to integer: type=0x3
Sounds like "This is a test" value being consumed for attr0
, although this attribute is not defined in the layout. Note that the application compiles correctly, but the result is incorrect.
Why am I getting this result?
source to share
No one has answered this question yet
Check out similar questions: