Issue
In an android project a custom color resource file created to change the background color of disabled button. Added a resource file /res/color/app_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorDisabled" android:state_enabled="false" />
<item android:color="@color/colorPrimary" />
</selector>
Used this resource in style /res/valus/styles.xml
<style name="Button" parent="TextFont">
<item name="android:background">@color/app_color</item>
<item name="android:layout_width">370dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/white</item>
</style>
Used the style in a layout like below:
<Button
android:id="@+id/button"
style="@style/Button"
android:layout_width="392dp"
android:text="@string/withdraw_in_cash"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
This code is working fine with simulated Nexus 5X API 29x86, but for the real device (moto e 6s) it crashes with an error like a blow:
2020-06-16 22:48:09.053 12963-12963/com.ans.realtimeredemption E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ans.realtimeredemption, PID: 12963
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ans.realtimeredemption/com.ans.realtimeredemption.SelectRedemptionType}: android.view.InflateException: Binary XML file line #61: Binary XML file line #61: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2979)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3114)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1871)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:235)
at android.app.ActivityThread.main(ActivityThread.java:6760)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
Caused by: android.view.InflateException: Binary XML file line #61: Binary XML file line #61: Error inflating class Button
Caused by: android.view.InflateException: Binary XML file line #61: Error inflating class Button
Caused by: android.content.res.Resources$NotFoundException: Drawable com.ans.realtimeredemption:color/app_color with resource ID #0x7f05001a
Caused by: android.content.res.Resources$NotFoundException: File res/color/app_color.xml from drawable resource ID #0x7f05001a
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:869)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:648)
at android.content.res.Resources.loadDrawable(Resources.java:897)
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:955)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
at android.view.View.<init>(View.java:5022)
at android.widget.TextView.<init>(TextView.java:892)
at android.widget.Button.<init>(Button.java:166)
at android.widget.Button.<init>(Button.java:141)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:71)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:67)
at android.support.v7.app.AppCompatViewInflater.createButton(AppCompatViewInflater.java:187)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:110)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:880)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:841)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:841)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:841)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:841)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.ans.realtimeredemption.SelectRedemptionType.onCreate(SelectRedemptionType.java:18)
2020-06-16 22:48:09.057 12963-12963/com.ans.realtimeredemption E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2959)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3114)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1871)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:235)
at android.app.ActivityThread.main(ActivityThread.java:6760)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:190)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122)
at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:855)
... 45 more
Emulator Details:
Name: Nexus_5X_API_29_x86
CPU/ABI: Google APIs Intel Atom (x86)
Path: C:\Users\Sowvik Roy\.android\avd\Nexus_5X_API_29_x86.avd
Target: google_apis [Google APIs] (API level 29)
Skin: nexus_5x
SD Card: 800M
Snapshot: no
hw.dPad: no
hw.lcd.height: 1920
runtime.network.speed: full
hw.accelerometer: yes
hw.device.name: Nexus 5X
vm.heapSize: 256
hw.device.manufacturer: Google
hw.lcd.width: 1080
hw.gps: yes
image.androidVersion.api: 29
hw.audioInput: yes
image.sysdir.1: system-images\android-29\google_apis\x86\
tag.id: google_apis
hw.camera.back: emulated
hw.mainKeys: no
AvdId: Nexus_5X_API_29_x86
hw.camera.front: emulated
hw.lcd.density: 420
avd.ini.displayname: Nexus 5X API 29 x86
hw.arc: false
hw.gpu.mode: auto
snapshot.present: no
hw.device.hash2: MD5:ae200ad6786ec467cb9067f7b46b0fd1
hw.ramSize: 1536
hw.trackBall: no
PlayStore.enabled: false
hw.battery: yes
hw.cpu.ncore: 4
hw.sdCard: yes
tag.display: Google APIs
runtime.network.latency: none
hw.keyboard: yes
hw.sensors.proximity: yes
disk.dataPartition.size: 800M
hw.sensors.orientation: yes
avd.ini.encoding: UTF-8
hw.gpu.enabled: yes
The screenshot of the structure is attached below:
Solution
Change your app_color.xml file from this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorDisabled" android:state_enabled="false" />
<item android:color="@color/colorPrimary" />
</selector>
To this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorDisabled" android:state_enabled="false" />
<item android:drawable="@color/colorPrimary" />
</selector>
You are supposed to use drawable not color
Hope you got a difference...
Answered By - AgentP
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.