Android R.java gives error on Numeric (media) file name - android

I have huge number of sound bytes which I want to use in my project. Unfortunately all files are named numerically like "001.m3 , 002.mp3 ...."
When I added files in raw folder, Android R file gives error.
How can I solve this problem. Can any one provide me link where android has mentioned naming conventions for resource files.

Every resources having entry in R.java file, If you see R.java file is nothing but like our normal class
public final class R {
public static final class raw {
public static final int 001=0x7f090005; // this will not accept as a variable name
}
public static final class drawable {
}
public static final class id {
public static final int main=0x7f090001;
}
}
You should follow the same Naming Convention like we do have for Variables i.e.
1) Must not start with number
2) Must not contain Special character except(_)
3) Must not used Reserved Keyword mentioned here
Solution: You have to rename you files, thats it.

The problem that you are facing is because of your file names, since your file name is 001.mp3 or 002.mp3. Android creates R.java file automatically, and in that file(R.java) it will create a variable by that file name which is variable name "001". Having a numeric variable name is wrong . It will not allow such thing and instead throw an error.
If your file is 001.mp3 then R.java will have error in this line which is
Syntax error on token "001", invalid VariableDeclaratorId
public static final int 001=0x7f050000;
I request you to change your file names. May be follow the recommendations Are there conventions on how to name resources?

Related

How to store resource file with same name but different extensions in android

I am working on one static custom Android Media Player Application where i have many media files stored in raw folder.
Where as some files have same name but different extensions, here android is giving me
res\raw\then.mp4:0: error: Resource entry then is already defined.
res\raw\then.mp3:0: Originally defined here..
Can anybody please suggest me something on this?
Also there are few media files with java keywords like if, else, return, switch, case,class, else, final, long, new, this,true... where android is giving me error of invalid symbol.
Please suggest me solution for that also.
Thanks in advance...
As #blackbelt said, it's not possible, I'll just add that you can instead put your files in the assets directory instead of res.
You will not be able to use them like R.id.file, but you will get more flexibility.
simply you can't. Raw is build at compile-time inside R.java, and the name key must follow the java convention for naming. Since
if, else, return, switch, case,class, else, final, long, new,
this,true
are reserved keywords you can not use them.
Edit: R.java would look lie:
public static final class raw {
public static final int if=0x70000;

How to create a string resource programatically

I am using WebTrends analytics in my app.
(Ref-http://help.webtrends.com/en/android/)
The WebTrends APIs are written in such a way that the initialization happens ONLY from resources file. They provide webtrends.xml with the sdk and all the parameters are set from the values from this xml file.
Now the problem is I have to set a couple of values dynamically based on our server feed.
Here I have only two choices:
(1) Set WebTrends initialization values in the code which looks impossible from WebTrends SDK. Neither the member variables are exposed outside the library nor there are any setters/methods to set the initialization params.
(2) Create resources from webtrends.xml dynamically or at least set the values for resources dynamically which also seems impossible.
Can anyone please suggest the way out of this deadlock?
You cannot do that. When you add a string resource an automatic entry is made for the resource in R.java file at compile time.
Example:
public static final class string
{
public static final int app_name=0x7f040000;
}
where app_name is the name of the string resource. So, it is not possible.

Why can't I use the string ID android.R.string.share?

It's right there in values/strings.xml in Android's source - why isn't it in android.R?
edit: I am referring to Android's built in string resources (android.R.string.…) and not my own project's resources - so, for example, while I can use android.R.string.cancel, I can't use android.R.string.share despite the fact that the two of them are declared in the same file in Android's source code
They are just resources under the resource folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="share">hello</string>
</resources>
The above resource has a entry in R.java file which in under your package name. You can open R.java and have a look at it.
public static final int share=0x7f040000;
You can refer to the string in Strings.xml as
R.String.share
The R.strin.share is an int.
You can also get the string as
String s= getResources().getString(R.string.share);
Edit:
http://developer.android.com/guide/topics/resources/accessing-resources.html#PlatformResources.
Check the topic in the above link under the heading Accessing Platform Resources
Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the android package name.
For list of all resources check the link
http://developer.android.com/reference/android/R.html
The list from http://developer.android.com/reference/android/R.string.html
You are compiling for which SDK version??
Please check strings.xml of same SDK version..I think there is some mismatch in target SDK version and strings.xml(you are referring to, where "share" is present)
However i will recommend to add your own "share" string in application strings.xml, to avoid unwanted issues due to Android Fragmented nature.
android.R is the system's R
Your R is just R within your own package name. So just call it R.string.share instead of android.R.string.share when referring to it.
It's because it is not defined in android.R.string, but in com.android.internal.R$string. you cannot access it without some 'hack'.
public static int getReflactField(String className,String fieldName){
int result = 0;
try {
Class<?> clz = Class.forName(className);
Field field = clz.getField(fieldName);
field.setAccessible(true);
result = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
...
int share_id=getReflactField("com.android.internal.R$string", "share");
if(share_id!=0)
Toast.makeText(getContext(), ""+getResources().getString(share_id),0).show();
this works both on my android 4.4 and 8.0 device
Or simply use
getResources().getString(Resources.getSystem().getIdentifier("share","string", "android"))

how do you find out what android resource is 0x7f040000 in eclipse?

Over the course of programming I get errors that give a resource number like "0x7f040000" (or sometimes in decimal form) My question is simple: Is there an easy way to tell what resource that is in eclipse?
i know i could manually identify every resource and print them out, but then every time i make a change to the program i'd have to update this code. Is there some way to search by resource ID?
YOu look up the R class in the gen folder
for example
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int block=0x7f020000;
block is part or the drawables in the R file.
if you want to do this at runtime (and not just in the IDE of eclispe you can try
getResources().getResourceName(id);
you can use getResources() for that:
getResources().getResourceName( theID )
In your project structure, you should have a directory called gen. Browse that directory until you find the class R. There you find every resource of your project defined.
Another possibility is to go directly to the R class: Press ctrl + shift + T and in the popup you just type R. But be careful: There are at least two R classes listed there. One is from Android itself (package android.R) and your with the package name you have chosen.
Look at your R class and see what variable corresponds to that resource id. Right click -> Go To Definition.

Whence construct R.id?

In some code we're trying out, coming from various tutorials, we end up missing R.id, which should be generated in R.java, obviously. We can add it in (by analogy with other "first" Android projects we've done), but as this file is auto-generated, anything we do like that just gets overwritten ultimately.
public static final class id
{
public static final int Button01=0x7f060004;
.
.
.
}
Was there a construct to put into strings.xml, main.xml, etc. that causes this to be generated?
(Yeah, we're total noobs. Sorry.)
Thanks for any help you can give,
Russ Bateman
Say I have an XML file with the following content:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView android:id="#+id/ListView01"
/>
</RelativeLayout>
I save it in res/layout/ .
Then R.id.ListView01 gets automatically created.
You might want to look at the Notebook Sample application and how it's organised. At least that's how I got familiar with androids organisation.
R.java is auto generated by Eclipse. If it is not built, this means you have probably an error somewhere in your xml files, or a resource with a name that is not allowed.
Sometimes it is just Eclipse doing strange things, in this case, you can try :
Project > Clean > all project
Then let Eclipse work.
Sometimes it solves the issue. If not, it's highly probable that you have an error somewhere in your resources.
To create this file, Eclipse gathers the ids you declared in your xml files with #+id,but also the layout names, images names, string names, ...
It appears that project/res/layout/main.xml contains the constructs that lead to the generation of id in R.java. (I'm not limiting the source for these only to that XML file.)
Look specifically for android:id in various widgets (I think they're called) such as TableLayout, TextView, EditText, etc. There will be corresponding
public static final class id
{
public static final int x=0x7f05006;
public static final int y=0x7f05000;
public static final int z=0x7f0500c;
.
.
.
}
where x, y, z correspond to the (TableLayout, TextView, EditText, etc.) identifier coded immediately after the "#+id/" construct in the XML file (and the initialized value in R.java is magically generated--you don't have to worry about that).
Thanks to all who contributed.

Categories

Resources