Android / Eclipse / Premature end of xml file

24,172

Solution 1

And I solved it. Man, what a goofy thing to have confound me for two days! I cleaned the project, rebuilt the xml file, and it works fine. I have no idea what was plugging things up as I had never actually tried to run the project so there should not have been an issue with out.out, but it worked. Thanks to all who offered advice. I truly do appreciate your time and attention in helping me with this one.

Solution 2

You might look at this answer.

That person's problem was that the file was not created as an XML file, from Eclipse's point of view. Try File > New > Other... / select XML.

Solution 3

Try

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
        android:drawable="@drawable/dategrey" />
    <item android:drawable="@drawable/datewhite" />
</selector>

You need to check the state first then call the specific drawable.

Share:
24,172
Blind Fish
Author by

Blind Fish

Updated on July 09, 2022

Comments

  • Blind Fish
    Blind Fish almost 2 years

    Let me start by acknowledging that I did find a similar question on here but there was only one answer and that answer did not apply in my situation, so...

    I am just now wading into Android and have come upon a sticking point. I am using Eclipse and have run into a problem trying to create a state-list drawable and keep getting a "premature end of file" error in Eclipse. I have the images in my "drawable" directory. I create a new file, name it date.xml, and type the following:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/dategrey" android:state_selected="true" />
        <item android:drawable="@drawable/datewhite" />
    </selector>
    

    The exact error that I am getting is "Premature end of file. Error parsing XML: No elements found.". The has me confused as there is clearly an element in there. What am I missing?

  • LarsH
    LarsH over 12 years
    That would be strange... XML consumers are not supposed to care about the order of attributes.
  • LarsH
    LarsH over 12 years
    Are you thinking of the noNamespaceSchemaLocation attribute? The namespace URI for the android namespace (to which the root element does not belong) is not a URL for an XML schema for the document.
  • Aaron Digulla
    Aaron Digulla over 12 years
    Well, it's a URI but I've had trouble with some XML parsers trying to download it. Also the URI might be mapped to a valid URL in your XML catalog.
  • LarsH
    LarsH over 12 years
    An XML parser that tries to download every namespace URI declared in the document? And then throws an error if it can't? Something is seriously wrong with any XML parser that behaves that way.
  • Andrei
    Andrei over 12 years
    You are right the order of the attributes is not important. However, the order of the entries is important when dealing with drawables.
  • Blind Fish
    Blind Fish over 12 years
    I thought that this might be the case as well, but creating the xml file via Eclipse in exactly the manner you suggested produced the same result.
  • Blind Fish
    Blind Fish over 12 years
    Checking state first had no effect. I copied and pasted what you had above and still get the premature end of file and parsing xml errors.
  • Andrei
    Andrei over 12 years
    Have look at this blog post on drawables. It might help you get a better understanding of how the XML structure should look. I have a feeling that could be the problem in your case.
  • LarsH
    LarsH over 12 years
    @Andrei: Re: "but the order of the entries is important" - but the order of items in your suggestion is the same is what the OP had. Am I missing something?? Also, order of entries may affect drawable behavior, but should not cause XML parsing errors.
  • Blind Fish
    Blind Fish over 12 years
    I appreciate the link as it will come in handy in improving my design abilities, but there is absolutely nothing there that answers my question. My XML is well-formed even by the examples in the link. I have looked at countless android tutorials and examples on how to make a state-list drawable and each one uses the exact form and syntax that I have used. I read the entire link that you posted twice and found absolutely nothing that was helpful in this situation. Did I miss something?
  • LarsH
    LarsH over 12 years
    Glad you found the answer. Really strange that recreating the XML file under a different name didn't fix the problem. That's just wrong.
  • Kishore
    Kishore over 9 years
    Lol such a brilliant answer (y)