Eclipse and xml files in Android - android

For reasons unfathomable to me, sometimes I edit an xml file (such as res\layout\main.xml) and Eclipse stops recognizing it. I get errors like "Bad XML block: header size 93 or total size 0 is larger than data size 0".
If I delete the contents and retype the xml file everything works fine.
Any ideas what's going on?

Try out both this things ,this can be help you to resolve the problem:
I just ran into the same error message. In my case, the XML was referencing a missing icon file in the drawables folder.
or
A Project->Clean should take care of this.

See this problems usually comes because of menu -
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/save_tea" android:title="#string/save_tea_label" />
</menu>
Better if you are not using menu so remove it otherwise give the proper string information there.
Thanks

Related

Android: How to tell ShrinkResources to keep certain resources

I have an mp3 and an ogg file in my res/raw folder which I used with Android Media Player. I used setDataSource like so
musicPlayer.setDataSource(context, Uri.parse("android.resource://com.me.myapp/" + R.raw.music));
This works fine when running from Android Studio, however, when exporting a signed APK with shrinkResouces enabled, it strips out the mp3 (as mentioned above, I also have an ogg file which I use in the same way and which also gets stripped out). Here is the associated results in the Resouces.txt file:
Skipped unused resource res/raw/music.mp3: 485531 bytes (replaced with small dummy file of size 0 bytes)
Skipped unused resource res/raw/oggmusic.ogg: 5335764 bytes (replaced with small dummy file of size 0 bytes)
I've done some research and there is a bug report on the Google Issue Tracker for this problem, however the poster says he/she used the information contained in the above link to make sure the required resources were not removed and the issue has been marked as 'intended behaviour' (which I find a little odd as I wouldn't have thought it would be intended behaviour for required resources to be removed).
Anyway, I've tried to do as mentioned in the docs and it doesn't seem to work, what am I doing wrong here? I've created an xml file called 'keep.xml' and placed it into my raw folder alongside the resources to keep.
This is the XML:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="#raw/*.mp3, #raw/*.ogg"/>
This issue wasn't evident in Eclipse, but has appeared since migrating to Android Studio.
EDIT
I did a bit more testing and I can confirm that whatever I specify in the XML file is being completely ignored. If I use "discard" for example instead of, or in place of 'keep', the resources stated in 'discard' appear in the generated APK file, so I must be doing something wrong or missing a step out somewhere.
I just experienced this issue and have found out what I was doing wrong.
Do not specify file extensions when referring to resources.
What I did at first (this will not work):
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="#raw/vid.mp4"
/>
Solution, drop the file extension:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="#raw/vid"
/>
You can verify this by inspecting the resources.txt file that you can find at ./app/build/outputs/mapping/<buildType>/resources.txt
https://developer.android.com/studio/build/shrink-code

ClassCast Exception Android Studio

I am getting a ClassCastException in android studio while running my project. Can anyone tell me what is causing this error.
ClassCastException: com.intellij.psi.impl.source.PsiPlainTextFileImpl cannot be cast to com.intellij.psi.xml.XmlFile
Thanks.
Sovled this atlast. This Question helped me. Android Studio ClassCastException When Attempting to Open A Layout XML File . I had a 11MB xml file in values. That was the problem. After deleting that, the problem was solved.
And if you want to use a large XML file add the below code in idea.properties and vmoptions in the bin folder of Android Studio.
**Add in idea.properties**
#------------------------------------------------------------- --------
# Maximum file size (kilobytes) IDE should provide code assistance for.
# The larger file is the slower its. editor works and higher overall system memory requirements are
# if code assistance is enabled. Remove this property or set to very. large number if you need
# code assistance for any files available regardless their size.
#---------------------------------------------------------------------
idea.max.intellisense.filesize=999999
**Add in vmoptions**
-Didea.max.intellisense.filesize=999999 # <--- new line
Reference: https://stackoverflow.com/questions/23057988/file-size-exceeds-configured-limit-2560000-code-insightfeatures-not-availabl
It would be better if you can share some code snippets...
Anyways, as far as ClassCastException is concerned, it means you are declaring a variable of some type and assigning it to another type you have defined in a layout xml file...
for example, in the xml you may have had:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:id="#+id/btn1"
android:layout_width="wrap_content">
</Button>
</LinearLayout>
but while connecting the component to code:
ImageView img1 = (ImageView)context.findViewById(R.id.btn1);
This will fire a ClassCastException because you are casting a Button to an ImageView variable which is as you understand not possible!
If this doesn't solve your problem then it would be better if you post some code snippets after figuring out which code snippet causes the error!

Android Studio: Error parsing XML & URI is not registered

After adding a new Activity to my project I get following error while compiling the layout
Gradle: Error parsing XML: not well-formed (invalid token)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_password"
android:text="<password>"/>
</LinearLayout>
"http://schemas.android.com/apk/res/android" is marked red and hovering pops following message up
URI is not registered ( Setting | Project Settings | Schemas and DTDs )
My Settings>Project Settings>Schemas and DTDs look like that:
I already found some similar questions around here but nothing helped me to solve this issue. I hope someone has an idea... :)
I was having a similar issue. However, the comments did not assist me in the solution to this issue. This answer from this question actually solved my problem.
Summary:
Go to "File > Project Structure > Modules", click "add" and then click "android" and "apply/ok". That should solve anyone having a similar problem but the comments for this question are not helping you.
For me, the problem occured when I created my own subfolder for old code.
Specifically, Android Studio threw an error for all layout files that weren't in the default folder ../res/drawable/, but instead placed in my own subfolder ../res/drawable/backup.
I got the same error for a long time and none of the answers I found online really helped me, or probably I wasn't looking the right way. At the end what I found out was the way I named the directory. So, I was trying to make the landscape layout for my app and named it layout_land. That kept showing me the URI is not registered error. I just had to change the directory name to layout-land.
Summary: No underscores in directory names!
My issue was creating new resource directory for some animation work. The files were .xml and I chose file type as "xml". I got this problem. Changing the file type to "values" and the problem went away. No idea why, could someone explain this to me? I'm using Android Studio on an iMac.
GO to settings >Language&Framework >Schemas&DTD
here add Uri used in your code.
Some special characters not allowed here.
Directly to assign text then with warning
android:text="value"
#string file from get then avoid warning
android:text="#string/hello"
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello!</string>
</resources>
For me this problem was gone when I have made following changes to my project structure on Android Studio.
file->project structure ->SDK Location -> enable check mark for "Use embedded JDK (recommended).
Hope this will help someone.
First thing is go to the project folder in the Android studio and check for any folders which are created by you. It will show this error if you have created any folder on your own.

stop xml entity from producing square

i have the following in my xml file and it gives me a square ⃠
<?xml version="1.0" encoding="utf-8"?>
...
<Button android:text="&#8416" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
...
the character is the "no symbol"/prohibited symbol but it shows up like a square...other symbols show up fine like euro €.
question is how do i get the symbol to show up and if i cant, how can i know what symbols will and wont work....is there a master list somewhere
Ok so due to various reasons it won't show up and there are other posts on this.
Why not use a left drawable in the image by:
android:drawableLeft="#drawable/myicon"
Same result and you don't have to worry about character encoding and fonts.
Sometimes it depends on the Font you are using. I had a link to look that up. I'll get back here as soon as I find it.
Update:
Here you go - http://www.fileformat.info/info/unicode/font/index.htm

Android XML Viewer failing

I've been having this problem for a while:
Here is my header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/logo_text"/>
</LinearLayout>
And this what the Eclipse/Android XML viewer is giving me:
Unable to resolve drawable "C:\Users\Me\MyApp\res\drawable-hdpi\logo_text.png" in attribute "src"
This happens in other projects as well for seemingly random images. Project->Clean doesn't help either. I know this has to be an issue with my machine or eclipse installation because it doesn't happen on my work machine, only my home laptop.
There are no problems when the app is installed, but it would be nice to be able to see layout changes without having to install the app over and over again.
EDIT
I also get these messages anytime I use a custom View in a layout file:
error!
UnsupportedClassVersionError: Bad version number in .class file
Unable to resolve drawable "C:\Users\Me\MyApp\res\drawable-hdpi\logo_text.png" in attribute "src"
Exception details are logged in Window > Show View > Error Log
I sometimes have weird issues like this when importing new images into my project. Do you have any other res-image directories with different versions of this file? You may try removing the png and re-adding to the project.
The error message explicitly mentions the -hdpi version of the drawable directory. Have you got that image in all of the different versions of the drawable directory? Perhaps it is in res/drawable-ldpi or res/drawable-mdpi but not res/drawable-hdpi?
If you don't already have one, you could create a plain res/drawable folder and add the image there, then one would expect the system to fall back to that whenever it didn't find a definition-specific version.
Actually try to put an id tag on ImageView : android:id=:"#+id/imageView".Tell me if it is working or not.

Categories

Resources