2011-04-07 57 views
4

問題:僅文字顏色的HTC Desire HD的失敗

我的應用程序適用於大多數設備很好,但在HTC Desire HD的,崩潰(安卓2.2.1)。 (編輯:錯誤依然存在於2.3.3上)

異常類 - android.content.res.Resources$NotFoundException

源法 - Resources.loadColorStateList()

詳細信息:

我使用的主題設定文字顏色。我宣佈在佈局XML文件中的TextView的文字顏色...

<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"/> 

,並宣告文字顏色爲每個主題應該在資源主題文件是什麼?

<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> 

如果我只是在我的xml文件中對textColor進行硬編碼,它工作正常,所以它似乎無法理解'?textColor'應該讓它看起來像我的themes.xml文件。再次,這在我測試過的其他設備上正常工作,但在HTC Desire HD上失敗。

錯誤:

堆棧跟蹤是相關的位......

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

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) 

編輯:的Eclipse 3.5.2報告給我「無法解析我的layout.xml的圖形佈局選項卡中屬性「textColor」中的「?textColor」顏色值。不過,我仍然可以編譯和構建,但它仍然可以在HTC Desire HD以外的其他設備上使用。完整的代碼在這個問題的底部。

編輯:2011年7月1日

我已經注意到,我有兩個獨立的堆棧跟蹤(雖然它僅是附近的變化,我認爲年底1號線)...

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 

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 

編輯:7月4日 - 全碼

的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> 

編輯:5日2011年7月我發現,如果我使用「機器人:textColorPrimary」而不是「文字顏色」,錯誤不再出現在HTC Desire HD的,但文字顏色將始終保持系統textColor默認。在所有設備上,它可以按預期完美工作。

編輯:2011年7月6日清晨基本上,HTC Desire HD似乎忽略用'?'設置的任何值。儘管在上次編輯中描述了一半修復,但我稍後在使用樣式的頁面上的應用程序中遇到了錯誤。

編輯:2011年7月6日午後我有一半通過粗略設置Java中的文本顏色(取決於主題)來解決問題。但是,這還不適用於我的ListView中使用'樣式'的文本。不過,我現在發現,儘管使用'?textColor',我的程序的其餘部分仍可以正常工作!這意味着只有開幕式的屏幕無法正常工作,這表明HTC Desire HD並不足以及早創造出足夠的主題或風格來破解'textColor'! (或類似的東西!)這可能是至關重要的信息!我可以在我的代碼中調換任何東西來使其工作嗎?

回答

0

我最終選擇通過修改我的一些xml和Java文件來解決這個問題。這並不理想,因爲它會在代碼中造成不一致,並且會過分複雜化主題。然而,爲了迎合HTC Desire HD用戶,我已經屈服於它。如果有人可以找到替代解決方案,請在此處發佈。

寬泛地說,我定我的代碼通過Java靜態方法,如本內設置文本顏色...

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); 
    } 
} 

或創建(每個文本顏色之一)styles.xml內的各種風格和在Java代碼中調用相關的代碼。

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

此片段不是必需的。主題應該在沒有明確指定佈局中的樣式的情況下生效。

android:textColor="?textColor" 
+0

感謝您的答覆吉姆!它不工作,我害怕壽。如果我刪除該行,文本將返回到默認顏色。如果我從幾個月前寫這個的時候就沒有記錯,它需要聲明**「?textColor」**以確保它使用主題作爲文本顏色。當然,它可以在我嘗試過的所有設備上正常工作(如果我保留此片段)。我想知道我是否設置了與你想象不同的東西?任何其他想法?謝謝。 – 2011-04-07 13:41:42

+0

(在上面的評論,我的意思是說,原來的代碼,我已經試過了所有*其他*設備正常工作)。 – 2011-04-08 09:24:01

+0

凹凸。這仍然是一個問題。有沒有人有更多的建議?謝謝。 – 2011-04-11 09:38:41

0

「?」語法是引用當前主題中的樣式,您是否在Manifest中設置了主題?我沒有在你提供的代碼中看到它,所以我假設它是否已設置,它在那裏完成。 (這也許是其他Jim評論的原因 - 你的迴應表明它沒有被設置)。

此外,你已經定義了一個「textColor」屬性兩次 - 一次在themes.xml中,一次在attrs.xml中。任何設備如何知道您指的是哪一個?因爲你正在使用「?」語法,系統應該知道尋找一個樣式屬性,但我不認爲它顯式檢查資源類型。實際上,有效的語法包括資源類型,它可能有助於具有「?style/textColor」。另外,請嘗試調用另一個「attrTextColor」以區分它和主題中的「textColor」屬性。 (另外,「textColor」在我看來似乎是一個不好的屬性名稱,因爲它可能與屬性「android:textColor」混淆 - 也許你應該將其命名爲「txtoolsTextColor」或其他東西。由於系統應該知道要看你的包對於「textColor」資源,它真的不應該...除了你有一個bug,這可能是原因...)

+0

嗨,吉姆,爲您的迴應歡呼!也許我沒有明確的是,應用程序的主題是由用戶選擇的。目前有兩種選擇;一個輕的和一個黑暗的。因此,主題不在清單文件中設置,它由用戶在應用程序的「首選項」屏幕上選擇,並在Java中用此代碼設置 - activity.setTheme(R.style.Theme_TxttoolsLight);關於您的themes.xml/attrs.xml查詢,每個主題的顏色在themes.xml中定義,而需要attrs.xml來聲明主題中的屬性。 attrs.xml不定義顏色本身。 – 2011-08-08 08:10:16

0

最近我遇到了類似的問題。對於只有銀河S 4G充氣視圖時,我收到以下錯誤:

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

此錯誤代碼是非常無益的,它原來是更有用看堆棧跟蹤的頂部,在那裏我發現:

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

然後看着在行804所討論的XML文件,我發現它與Android的設置問題:cacheColorHint。雖然我有一個自定義主題,但它並不適用於我的ListView,並且cacheColorHint只是簡單的硬編碼。修復很簡單,就像上面在java:listview.setCacheColorHint中動態設置它一樣。

它似乎在java中的某些觀點變量,而不是XML往往導致更少的問題,也有瓷磚位圖中的錯誤也由seeting瓦性能dynamicallyr粥比Java的解決(見XML drawable Bitmap tileMode bug?

對於某些手機和佈局模型,似乎好像對所有XML進行解碼之前的佈局都是膨脹的。但這是猜想,或者至少是探討這些問題的啓發式方法。

1

你提到你的意見,你是在活動呼籲setTheme,而不是設置主題,在該清單的活動。請確保在super.onCreate調用之前調用setTheme,並在該Activity的onCreate生命週期方法中調用setContent調用。這將防止在主題之前使用任何默認主題。

我懷疑正在發生的事情是,你在你的佈局XML使用「?文字顏色」,但是這個值只在您的自定義主題,並在某些設備上的默認主題definied。其他設備在默認主題中沒有定義的值。

的錯誤消息是越來越擺在那裏的一些值,它最終要解決,不能用作文本​​顏色的資源。文本顏色通常是一個XML狀態列表,它指定了用於某些狀態的顏色,例如焦點狀態,但如果您希望所有狀態的顏色相同,則有時會使用直接顏色資源。

就像我在其他評論中提到的,我見過的大多數主題不直接有文字顏色值。相反,他們有一個textViewStyle指向一個樣式,然後有一個textColor。如果你想影響更多的東西,你可能想嘗試colorForeground。通常textViewStyle將爲textColor指定一個XML狀態列表文件,然後它將使用colorForeground作爲某些狀態的顏色。

相關問題