Valid XML file gives “failed to parse” error in An

2019-01-26 17:15发布

问题:

I have made a XML file:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:state_pressed="true"
   android:drawable="@drawable/calender_h" />
  <item
   android:state_pressed="false"
  android:drawable="@drawable/calender_n" />  
</selector>

but it is giving me the following error:

Failed to parse file G:\eclipseHeliosWorkspace\WineCountry\res\drawable\calendar_selector.xml

What could be the problem?

回答1:

I'm not sure it's the same, but Similar issue happened to me when updating to ADT 15, in a library project. this is the google issue that currently still opened: 21046.

That's what worked for me:

  1. Remove the library declaration of the project.
  2. Open the layout causing the error (the one which using the selector) in graphical layout.
  3. Clean the project (Project -> Clean)
  4. Close and reopen eclipse. (the layout should be ok now)
  5. You can set the project as library again.

Hope this helps! (if it's still relevant...)



回答2:

I've had the same problem too, and there were no problems in Eclipse->Problems for me either.

You might consider adding in a default state for the drawable (as practiced in this ImageButton example at the Android Developers website), shown in this modified version of your presented code:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_pressed="true"
    android:drawable="@drawable/calender_h" />
<item
    android:state_pressed="false"
    android:drawable="@drawable/calender_n" />  
<item 
    android:drawable="@drawable/calender_n" />
</selector>

Otherwise, I would say (from something I've experienced just now in Eclipse) that you should push the app out to your phone or emulator anyway. In Eclipse, I'm staring at a "Failed to parse..." message in my Graphical Layout tab of the layout XML file I am having troubles with, but on my phone, I see the image exactly as it should be (responds perfectly to button presses and whatnot).



回答3:

You XML is fine. Are you missing the drawables? Try cleaning the project once. If that doesn't help, there might be some invisible special characters in the XML if you have copy-pasted this from somewhere.

Anyway, Look at the problems window. It should give you more details on the error.



回答4:

From Eclipse, right click the xml file and choose Open With -> XML Editor. In this, choose the Design tab. You may notice there is a mysterious child element after your <item>'s which is just empty. Right click on it and choose remove.

Just spent an hour or so trying to fix this problem and this turned out to be the fix, probably some invisible characters that cause parsing to fail. Anyway, hope that helps anyone else having this problem!



回答5:

  1. Failed to parse XML file for android
  2. check that your drawables exist at the appropriate paths
  3. check for inivisible characters

4. I`ve also ran into the same problem. What I found is that each of the StateListDrawable states (pressed, selected, focused, etc.) actually expects a valid drawable (png, jpg, gif, etc.). So, that means, something like this will cause the parse error problem:

<item android:state_selected="true" />



回答6:

I also ran into this. The layout xml files and main.xml would not parse. if you chose the graphical view for main.xml you would not get any buttons (in my case). What i found is that Eclipse was set to C++ rather than java. Setting it to the java perspective, closing, then reopening Eclipse fixed the issue.

Now Eclipse is doing other strange stuff like forgetting to connect to the phone intermittently.



回答7:

I had the same issue and it was due to the name of one of the resource images. The image was sent to me by a designer that introduced some characters not accepted by Android and even when I renamed it inside Eclipse, something "evil" obviously remained. Weird.



回答8:

In my case, the drawables (PNG files) were corrupted while transferring them from Linux machine to my Windows machine where I do development. I figured that by trying to open the PNG files. Those PNG files could not be opened as they were corrupted.

After transferring them once again, it worked fine.