Installing textColor from a theme does not work only on HTC Desire HD

Problem:

My application works fine on most devices, but crashes on HTC Desire HD , (Android 2.2.1). ( Edit: Error in 2.3.3)

The exception class is android.content.res.Resources$NotFoundException .

Source Method - Resources.loadColorStateList()

detail:

I install textColor using Themes. I declare a textColor in a TextView in an XML layout file ...

 <TextView android:id="@+id/accountWarning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/accountWarning" android:textSize="20sp" android:layout_marginTop="10dp" android:padding="10dp" android:textColor="?textColor"/> 

and declare that textColor for each theme should be in the resource theme file ...

 <style name="Theme.TxtoolsDark" parent="Theme"> <item name="textColor">#FFFFFF</item> <item name="autoCompleteTextViewTextColor">#000</item> </style> <style name="Theme.TxtoolsLight" parent="Theme" > <item name="textColor">#000</item> <item name="autoCompleteTextViewTextColor">#000</item> </style> 

If I just transcode textColor into my xml file, it works fine, so it seems like it doesn’t understand that '?textColor' should make it look at my themes.xml file. Again, this works great on other devices I've tested on, but doesn't work on HTC Desire HD.

Error:

The corresponding stack trace bits ...

 android.view.InflateException: Binary XML file line #10: Error inflating class <unknown> 

and

 Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/scrollbar_handle_vertical.9.png from drawable resource ID #0x0: .xml extension required at android.content.res.Resources.loadColorStateList(Resources.java:1842) at android.content.res.TypedArray.getColorStateList(TypedArray.java:342) at android.widget.TextView.<init>(TextView.java:665) 

Edit: Eclipse 3.5.2 tells me "Unable to resolve color value"? textColor "in the" textColor "attribute on the Graphic Layout tab of my layout.xml. However, I can still compile and create, and it still works on all devices other than HTC Desire HD. The full code is in bottom of this question.

Edit: July 1, 2011

I noticed that I have two separate stack traces (although there is only one line near the end that changes, I think) ...

 java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.HomeTabs}: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown> at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3399) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3420) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) at android.app.ActivityThread.access$2300(ActivityThread.java:136) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:5073) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown> at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3399) at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170) at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:521) at android.app.ActivityGroup.onResume(ActivityGroup.java:58) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1260) at android.app.Activity.performResume(Activity.java:4011) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3389) ... 12 more Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:513) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) at android.view.LayoutInflater.inflate(LayoutInflater.java:407) at android.view.LayoutInflater.inflate(LayoutInflater.java:320) at android.view.LayoutInflater.inflate(LayoutInflater.java:276) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:217) at android.app.Activity.setContentView(Activity.java:1701) at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.createHomePage(Home.java:88) at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome.createHomePage(StandardHome.java:74) at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.onResume(Home.java:291) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1260) at android.app.Activity.performResume(Activity.java:4011) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3389) ... 18 more Caused by: java.lang.reflect.InvocationTargetException at android.widget.TextView.<init>(TextView.java:355) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:446) at android.view.LayoutInflater.createView(LayoutInflater.java:500) ... 33 more Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/scrollbar_handle_vertical.9.png from drawable resource ID #0x0: .xml extension required at android.content.res.Resources.loadColorStateList(Resources.java:1842) at android.content.res.TypedArray.getColorStateList(TypedArray.java:342) at android.widget.TextView.<init>(TextView.java:665) ... 37 more 

and

 java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.HomeTabs}: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown> at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2485) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2506) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1856) at android.app.ActivityThread.access$1500(ActivityThread.java:132) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:4293) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.RuntimeException: Unable to resume activity {uk.co.txttools.mobile.android.txttoolsmobile/uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome}: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown> at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2485) at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170) at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:521) at android.app.ActivityGroup.onResume(ActivityGroup.java:58) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242) at android.app.Activity.performResume(Activity.java:4004) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2475) ... 12 more Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown> at android.view.LayoutInflater.createView(LayoutInflater.java:518) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) at android.view.LayoutInflater.rInflate(LayoutInflater.java:626) at android.view.LayoutInflater.inflate(LayoutInflater.java:408) at android.view.LayoutInflater.inflate(LayoutInflater.java:320) at android.view.LayoutInflater.inflate(LayoutInflater.java:276) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:231) at android.app.Activity.setContentView(Activity.java:1742) at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.createHomePage(Home.java:88) at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.StandardHome.createHomePage(StandardHome.java:74) at uk.co.txttools.mobile.android.txttoolsmobile.pages.home.Home.onResume(Home.java:291) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1242) at android.app.Activity.performResume(Activity.java:4004) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2475) ... 18 more Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:415) at android.view.LayoutInflater.createView(LayoutInflater.java:505) ... 33 more Caused by: android.content.res.Resources$NotFoundException: Resource is not a ColorStateList (color or path): TypedValue{t=0x2/d=0x7f010001 a=-1} at android.content.res.Resources.loadColorStateList(Resources.java:1822) at android.content.res.TypedArray.getColorStateList(TypedArray.java:342) at android.widget.TextView.<init>(TextView.java:693) at android.widget.TextView.<init>(TextView.java:382) ... 36 more 

Edit: July 4th - Full Code

themes.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme" parent="android:Theme"> </style> <style name="Theme.TxtoolsDark" parent="Theme"> <item name="textColor">#FFFFFF</item> <item name="autoCompleteTextViewTextColor">#000</item> </style> <style name="Theme.TxtoolsLight" parent="Theme" > <item name="textColor">#000</item> <item name="autoCompleteTextViewTextColor">#000</item> </style> </resources> 

attrs.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <attr name="autoCompleteTextViewTextColor" format="reference|color" /> <attr name="textColor" format="reference|color" /> </resources> 

myLayoutFile.xml

 <?xml version="1.0" encoding="utf-8"?> <ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true"> <TextView android:id="@+id/accountWarning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/accountWarning" android:textSize="20sp" android:layout_marginTop="10dp" android:padding="10dp" android:textColor="?textColor"/> <TextView android:id="@+id/haveAccountQuestion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/haveAccountQuestion" android:textSize="20sp" android:layout_gravity="center_horizontal" android:padding="10dp" android:textColor="?textColor"/> <Button android:id="@+id/yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:width="80dp" android:text="@string/yesHappy" android:textSize="15sp" android:layout_gravity="center_horizontal"/> <Button android:id="@+id/no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:width="80dp" android:text="@string/noSad" android:textSize="15sp" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp"/> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:cacheColorHint="#00000000"/> </LinearLayout> </ScrollView> 

Edit: July 5, 2011 I found that if I use “android: textColorPrimary” and not “textColor”, the error no longer occurs on HTC Desire HD, but the text color will always remain the default value for textColor. On all devices, it works fine as expected.

Edit: July 6, 2011 Early morning In principle, HTC Desire HD seems to ignore any values ​​set using ??. Despite half the fix described in my last change, I get errors later in my application on pages that use styles.

Editing: July 6, 2011 Late Evening . I have half the problem resolved by roughly setting the color of the text in Java (depending on the topic). However, this still does not work for my text in my ListView, which uses a "style". However, I have now discovered that the rest of my program works fine, despite using "? TextColor"! This means that only the initial screen does not work, which indicates that HTC Desire HD does not create the necessary themes or styles early enough to decrypt “textColor”! (or something like that!) This could be important information! Can I swap my code to make it work?

+4
source share
5 answers

I eventually decided to fix this problem by reworking some of my xml and Java files. This does not seem ideal, because it creates inconsistencies in the code and unduly complicates what topics simplify. However, to satisfy the needs of HTC Desire HD users, I succumbed to this. If anyone can find an alternative solution, post it here.

In short, I fixed my code by setting the colors of the text in static Java methods such as ...

 public static void setTextColor(TextView tv) { if (CURRENT_THEME==TXTTOOLS_LIGHT){ tv.setTextColor(Color.BLACK); } else if (CURRENT_THEME==TXTTOOLS_DARK){ tv.setTextColor(Color.WHITE); } } 

or by creating different styles in styles.xml (one for each text color) and calling the appropriate Java code.

 SimpleCursorAdapter logins; if (Theme.getTheme()==Theme.TXTTOOLS_LIGHT){ logins = new SimpleCursorAdapter(this, R.layout.username_row_light_theme, c, from, to); } else if (Theme.getTheme()==Theme.TXTTOOLS_DARK){ logins = new SimpleCursorAdapter(this, R.layout.username_row_dark_theme, c, from, to); } 
0
source

You note in your comments that you are invoking setTheme in Activity, rather than setting the topic of activity in the manifest. Make sure you call setTheme before calling super.onCreate and calling setContent in this Activity onCreate method. This will prevent the use of any default theme before your theme.

I suspect that you are using "? TextColor" in your XML layout, but this value is only defined in your custom themes and in the default settings on some devices. Other devices do not have a specific value for the default theme.

Error messages are some value received there that ends with a solution for a resource that cannot be used as the color of the text. A text color is usually an XML state list that indicates the color used for certain states, such as focused, although if you need the same color for all states, a direct color resource is sometimes used.

As I mentioned in another comment, most of the topics I saw do not directly matter to textColor. Instead, they have a textViewStyle that points to a style that textColor then has. You can try colorForeground if you want to influence more things. Often textViewStyle will specify an XML state list file for textColor, which will then use colorForeground for color for certain states.

+1
source

This snippet is not needed .. the theme should take effect without explicitly specifying a style in your layout anywhere.

  android:textColor="?textColor" 
0
source

"?" syntax - refer to the style in the current topic, do you set your theme in the manifest? I do not see this in the code that you provided, so I assume that it is installed, it runs there. (And that is probably the reason for Jim's other comment - your answer indicates that it is not installed.)

In addition, you defined the textColor attribute twice - once in theme.xml and again in attrs.xml. As you know, any device you are accessing? Because you use "?" syntax, the system must know in order to search for a style attribute, but I do not think that it explicitly checks the type of resource. In fact, the actual syntax includes the type of the resource; it can help to have "? Style / textColor". Also, try calling another "attrTextColor" to distinguish between it and the "textColor" attributes in your theme. (Also, "textColor" seems to me to be the wrong attribute name, because it can be confused with the "android: textColor" attribute - maybe you should call it "txtoolsTextColor" or something else. Since the system needs to know to look in your package for the resource "textColor" it really doesn’t matter ... except that you have an error, and that could be the reason ...)

0
source

Recently, I ran into a similar problem. For Galaxy S 4G only, I received the following error when inflating the view:

 Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/scrollbar_handle_vertical.9.png from drawable resource ID #0x0: .xml extension required 

This error code is completely useless, it turned out that it would be more useful to look at the top of the stack trace, where I found:

 ComponentInfo{com.staircase3.opensignal/com.staircase3.opensignal.library.Tab_Overview}: android.view.InflateException: Binary XML file line #804: Error inflating class </code> 

Then, looking at the XML file on line 804, I found that this was a problem with setting android: cacheColorHint. Although I have a custom theme, it does not apply to my ListView, and cacheColorHint is simply hard-coded. The fix was simple, as the above was set dynamically in java: listview.setCacheColorHint.

It seems that some view variables in java rather than XML often lead to less problems, an error occurs with tiled raster images, which is also solved by defining the tile property dynamically than in java (see XML-drawable Bitmap tileMode error? )

It seems that for some phone models and layouts, the layout is bloated before all the XML has been decoded. But this is a hypothesis, or at least a heuristic approach to solving these problems.

0
source

Source: https://habr.com/ru/post/1347089/


All Articles