0

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!

Community
  • 1
  • 1
Miquel Perez
  • 445
  • 1
  • 6
  • 17
  • Do you have any other drawable resources (like pngs) named `default_button`? – Karakuri Apr 05 '16 at 04:41
  • No. default_button is only a xml file. There are no images with that name. Anyway as I said it's working on 2/3 devices. So if it was a major porblem it would probably fail in most devices, but it doesn't. I don't really know what's going on with this issue. It's being a pain. – Miquel Perez Apr 05 '16 at 05:15
  • Is there any common factor about the devices where this fails? (Android version? Manufacturer?) Just for the sake of curiosity, maybe try making your three shapes into separate XML drawables and use `android:drawable="..."` on each of your three item states in your selector drawable. – Karakuri Apr 05 '16 at 06:25
  • I only know one device where the app is not working, so I don't know common factors between devices that are failing. Thanks for the idea! Will check that! – Miquel Perez Apr 05 '16 at 13:18
  • which device app is not running and os version of the device – Brahmam Yamani Apr 05 '16 at 17:25
  • Try this , Create Two Different Drawable XML's 1) For Api level 21 and 2)For Below versions (below 21 api level) – Brahmam Yamani Apr 05 '16 at 17:31
  • That looks a reasonable way to fix it, I don't know why I didn't think about that before. Anyway I'm pretty new with it, how could I create a different XML drawable for each API? Putting it also into the drawable-v21 folder? Or adding some xml code to specify it? Thanks for the effort! I appreciate it! – Miquel Perez Apr 05 '16 at 21:44

1 Answers1

0

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.

Miquel Perez
  • 445
  • 1
  • 6
  • 17