Wednesday, March 7, 2012

[android-developers] Inflater crashes when using custom atrributes

Whole project with example is here: https://github.com/novemberox/ThemingSupport

I've got some custom attr in attrs.xml like:
<attr format="color|reference" name="myforeground" />
<attr format="color|reference" name="mybackground" />
<attr format="color|reference" name="foretoback" />

Latter I use them in my own theme:
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="myforeground">@color/foreground_tur</item>
<item name="mybackground">@color/background_tur</item>
<item name="foretoback">@color/foreground_to_background</item>
</style>

Then I can use those values in TextView that is in LinearLayout:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="foreground"
android:textColor="?attr/myforeground" />
And this works just great. But if I use the same TextView as ListView
element odd things start to happen.

If I pass Context to Adapter using "MyActivity.this" it works good,
but when I pass as Context "Activity.getApplicationContext()" inflater
doesn't see my custom attributes and fail with:
03-06 20:48:43.353: E/AndroidRuntime(951): FATAL EXCEPTION: main
03-06 20:48:43.353: E/AndroidRuntime(951):
android.view.InflateException: Binary XML file line #7: Error
inflating class <unknown>
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.LayoutInflater.createView(LayoutInflater.java:513)
03-06 20:48:43.353: E/AndroidRuntime(951): at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:
56)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.LayoutInflater.inflate(LayoutInflater.java:407)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-06 20:48:43.353: E/AndroidRuntime(951): at
com.test.ThemingTestActivity
$SimpleAdapter.getView(ThemingTestActivity.java:38)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.AbsListView.obtainView(AbsListView.java:1315)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.ListView.onMeasure(ListView.java:1109)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.View.measure(View.java:8171)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:
1012)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.View.measure(View.java:8171)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.View.measure(View.java:8171)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.View.measure(View.java:8171)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.ViewRoot.performTraversals(ViewRoot.java:801)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.os.Handler.dispatchMessage(Handler.java:99)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.os.Looper.loop(Looper.java:123)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 20:48:43.353: E/AndroidRuntime(951): at
java.lang.reflect.Method.invokeNative(Native Method)
03-06 20:48:43.353: E/AndroidRuntime(951): at
java.lang.reflect.Method.invoke(Method.java:521)
03-06 20:48:43.353: E/AndroidRuntime(951): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 20:48:43.353: E/AndroidRuntime(951): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 20:48:43.353: E/AndroidRuntime(951): at
dalvik.system.NativeStart.main(Native Method)
03-06 20:48:43.353: E/AndroidRuntime(951): Caused by:
java.lang.reflect.InvocationTargetException
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.TextView.<init>(TextView.java:321)
03-06 20:48:43.353: E/AndroidRuntime(951): at
java.lang.reflect.Constructor.constructNative(Native Method)
03-06 20:48:43.353: E/AndroidRuntime(951): at
java.lang.reflect.Constructor.newInstance(Constructor.java:446)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.view.LayoutInflater.createView(LayoutInflater.java:500)
03-06 20:48:43.353: E/AndroidRuntime(951): ... 31 more
03-06 20:48:43.353: E/AndroidRuntime(951): Caused by:
android.content.res.Resources$NotFoundException: File res/drawable-
hdpi/divider_horizontal_dark_opaque.9.png from drawable resource ID
#0x0: .xml extension required
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.content.res.Resources.loadColorStateList(Resources.java:1824)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
03-06 20:48:43.353: E/AndroidRuntime(951): at
android.widget.TextView.<init>(TextView.java:628)
03-06 20:48:43.353: E/AndroidRuntime(951): ... 35 more

So problem is that when we use custom attributes and then access them
using "ApplicationContext" they are invisible for it, while
"ActivityContext" sees them. Is it android bug, or am I missing
something?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment