我試圖在我的Android應用程序中實現顏色狀態列表資源,遵循Android開發人員指南Color State List Resource | Android Developers。因此,我在目錄res/color/
中創建了兩個資源文件button_text.xml
和,並在activity_main.xml
中分別使用@color/button_text
和@color/button_background
來引用它們。這裏的佈局代碼:
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:textColor="@color/button_text"
android:background="@color/button_background"/>
在運行應用程序,我得到在日誌中Resources Not Found Exception
如下圖所示:
Process: com.example.myanimation, PID: 29254
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myanimation/com.example.myanimation.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class Button
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class Button
Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.myanimation:color/button_background with resource ID #0x7f0b0053
Caused by: android.content.res.Resources$NotFoundException: File res/color/button_background.xml from drawable resource ID #0x7f0b0053
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:729)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:575)
at android.content.res.Resources.loadDrawable(Resources.java:854)
at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
at android.view.View.<init>(View.java:4177)
at android.widget.TextView.<init>(TextView.java:710)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:65)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:61)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1026)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1083)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.myanimation.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
我試圖清理和重建項目,但它沒有工作。此外,將xml files
置於res/drawable/
而不是res/color/
顯示相同的錯誤。請幫助解決該問題。提前致謝!
編輯:
這裏是button_text.xml
文件代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#000"
android:state_pressed="true"/>
<item android:color="#fff"
android:state_activated="true"/>
</selector>
而且,文件代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#fff"
android:state_pressed="true"/>
<item android:color="#000"
android:state_activated="true"/>
</selector>
貼自己的Android:文字顏色=「@ color/button_text」 android:background =「@ color/button_background」files –
你有沒有像'設置顏色#272c33 '在你'xml'文件中 –
Akshay
@Akshay我在問題中添加了'color/button_text.xml'和'color/button_background.xml'代碼@NileshRathod – Robin