简体   繁体   中英

Problems with XML drawables in Android

A couple hours ago I just posted this question here, and after a tough time I could find out what was actually going wrong among a big amount of drawables and Activities. But now is when I don't really understand why is it not working.

I created a xml drawable for one button, and then I defined its own style on the styles.xml file. The first that draws my attention is that when I call the xml drawable on the actual style, Android Studio cannot show an icon from it, so it shows a red exclamation instead (on the code section next to the line). Anyway, it works fine in most devices but I just found out that in some specific cases it throws a InflateException being caused by that xml drawable . I sincerely don't know what's wrong with the code I'm about to show.

This is the xml drawable ( default_button.xml ):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="5dp" />
            <stroke android:width="1dip" android:color="@color/green_temp" />
            <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/green_temp"  />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="5dp" />
            <stroke android:width="1dip" android:color="@color/black" />
            <solid android:color="@color/bright_green"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="5dp" />
            <stroke android:width="1dip" android:color="@color/bright_green" />
            <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/button_green" />
        </shape>
    </item>
</selector>

And then I set the style like this inside the Styles.xml file:

<style name="ButtonNormalText" parent="@android:style/Widget.Button">
        <item name="android:textColor" >@color/black</item>
        <item name="android:textSize" >25dip</item>
        <item name="android:height" >44dip</item>
        <item name="android:background" >@drawable/default_button</item>
          <item name="android:focusable" >true</item>
          <item name="android:clickable" >true</item>
</style>

The line that's throwing the error is: <item name="android:background" >@drawable/default_button</item>

It's weird because whenever I call some @drawable/xml_drawable it throws a:

FATAL EXCEPTION: main
Process: myapp.miquel.mqlapps.hombresmujeresapp, PID: 13239
   java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp.miquel.mqlapps.hombresmujeresapp/myapp.miquel.mqlapps.hombresmujeresapp.MenuActivity}: android.view.InflateException: Binary XML file line #43: Error inflating class android.support.v7.internal.widget.ActionBarContextView

Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class android.support.v7.internal.widget.ActionBarContextView

Caused by: java.lang.reflect.InvocationTargetException

Caused by: java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: <internal>: <nine-patch> requires a valid 9-patch source image

I sincerely don't know why is it working on most devices but failing in another one. Any ideas why? If you need to see some more code you can actually visit the link I provided at the beggining of this post. Thanks in advance!

Okay people, I finally fixed it by starting the app from zero. Just copied the original code into the new project and everything started working again like a black magic curse. I'm sorry I cannot come up with a more concret solution but that's sincerely how I fixed this weird bug.

All what I can say is that the R file seemed to have got corrupted at some point and I didn't realize it. So after creating a new project the app started to work again thanks to the new R file that wasn't corrupted.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM