I'm very new to Android development (have some Obj-C experience with Cocoa Touch though). I was testing my first Android app as I encountered these syntax errors:
Syntax error on token "100000", invalid VariableDeclaratorId
Syntax error on token "11", delete this token
Syntax error on token "2", delete this token
Syntax error on token "5000", invalid VariableDeclaratorId
Syntax error on token "61", invalid VariableDeclaratorId
Syntax error on token "69", invalid VariableDeclaratorId
When I double clicked them, they appeared to be in the file R.java and I have no idea how they are caused.
public static final class drawable {
public static final int 100000=0x7f020000;
public static final int 11ba=0x7f020001;
public static final int 2values=0x7f020002;
public static final int 5000=0x7f020003;
public static final int 61=0x7f020004;
public static final int 69=0x7f020005;
.....
It would be great if someone can tell how this is caused.
[updates]
MarsAtomic suggested that it is caused by not following the naming conventions for Android resources and perhaps having rawables named "5000", "69", which is, as a matter of fact, true in this case.
After changing the names, these exceptions didn't occur anymore.
But I would still like to know why having images in numeral names would trigger this. Thanks.
My comment has disappeared somehow, but... every language has naming conventions. Conventions are often just based on some semi-arbitrary heuristic because you have to have rules (e.g. it's slightly more clear that 'a1' denotes a variable name, whereas a variable named '1' could be confused for a value).
You can accept this answer as the canonical answer or not -- personally, I'm not bothered who gets what credit.
After changing the names, this exceptions didn't occur anymore.
But I would still like to know why this is happening.
Because the names of those resources are used as field names in an auto-generated Java source file (R.java), and they aren't valid Java field names. Java field names must start with a letter. Details on the characters allowed in field names here in the JLS.
(Really, MarsAtomic should be getting the credit for this, but doesn't seem to have seen the reposted question.)
You cannot declare variable names starting with numbers.
R.class is auto-generated, if you change something in it,it won't work correctly.
Thy to Clean and Build your Project.
If you get an error in
public static final class drawable {
the most common mistake is a named .png file in your res folder starting with a number.
For example 1_pic.png, 2015_22_22pic.png and so on.
Right way: pic_1.png
Among all folders located inside \ res \ and look for a file with the name different from the others, as 00000002. If you find, just delete it. Then click the name of your project in eclipse and then press the F5 key to refresh it. Thus the error will disappear and your R file will be updated.
Take a look at the RES directories. You might have a file with the numerical sequence mentioned above, eg 00000002. To solve the problem, simply remove it from the folder.
Related
I have a Java class that mainly contains strings. It does not have a layout as it is neither a Fragment nor an Activity. It is more used as an auxilliary class. I would like to assign the String values in the class by using the Resource strings as I would like to automatically translate the Strings. Howvever, I can't access the string resources from Java. I use the following code:
static String more = getString(R.string.more);
And in the XML file I have the ressource:
<string name="more">More</string>
I get the error message
Cannot resolve method 'getString'
I also tried static String more = getContext().getString(R.string.more);
but I got the error message:
Cannot resolve method 'getContext'
Would anyone mind helping me on that? I'd appreciate every comment.
Update: I tried to use the answer from "MikaelM"
Resources.getSystem().getString(R.string.more)
However, I get an "exception in initializer error" and when I use the initial String again, I do not get this error. So I still can't get the String from the ressource. DO you have an idea what I can do? I'd appreciate every further comment.
So the error is caused by "Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f120038"
The strange thing is that the resource in fact exists (I checked this several times).
getString(R.string...
is not the same as
Resources.getSystem().getString(android.R.string...
Only the second variant you can use in the static context. Alas, you can get this way only system resources.
If you need to get your own resources, no one-line solution exists. Use https://stackoverflow.com/a/4391811/715269 solution of #Cristian. And notice: it is a very good solution, even more, it is the solution meant to be used by the creators of Android.
You should be able to get your string with:
Resources.getSystem().getString(R.string.more)
See more here, getString Outside of a Context or Activity
I'm getting a "Conditionally Required Field Missing" error message, even though I'm sure that field is there.
58=Conditionally Required Field Missing, field=55
Versions:
QuickFixJ 2.1.0
FIX 4.4
Here is the FIX message that I'm sending (with mocked values and a few fields removed for clarity)
8=FIX.4.4
9=709
35=R
34=4
49=TARGET
56=ME
11=myClOrdID
131=myQuoteReqID
146=myNoRelatedSym
55=mySymbol // field missing
167=mySecurityType // field missing
Here is the calling code:
String symbol = quoteRequest.getField(new StringField(55)).getValue();
I also tried:
String symbol = quoteRequest.getString(55);
Here is my Data Dictionary:
<field number="55" name="Symbol" type="STRING"/>
I realize that the symbol field is no longer a part of the QuoteRequest FIX specification for 4.4 (though it was in earlier versions, such as 4.0), however surely there are ways to retrieve custom fields? I have no control over the QuoteRequest message that I receive.
I can always parse the message myself using toString() but that kinda defeats the purpose of using quickfixj in the first place.
Any ideas?
Tag 55 is inside the 146 repeating group. See the docs for reading repeating groups.
The symbol field is still in FIX44. You should spend some time familiarizing yourself with the FIX44.xml data dictionary file that you're using.
(You may find that you need to customize that file based on your counterparty's messaging; in practice, nobody uses the basic FIX44 message definitions without changing them at least a little.)
// create group
QuoteRequest.NoRelatedSym group = new QuoteRequest.NoRelatedSym();
// set group, confusing method name I find
message.getGroup(1, group);
// you now have all the getters of fields in that group
Symbol symbol = group.getSymbol();
I am learning Java and trying to make a basic Android application with Android Studio. When I try to add a TableLayout to an XML page the project will no longer compile because of errors in R.java.
Here are the errors:
Error: identifier expected
Error: illegal start of type
Error: identifier expected
These errors are repeated three times. Here are the lines it occurs on in R.java:
public static final class id {
public static final int 1=0x7f0b0086;
public static final int 2=0x7f0b0087;
public static final int 3=0x7f0b0088;
I'm guessing it fails because 1, 2, and 3 are not valid variable names, but I cannot change anything about a Java build file.
I found that others with similar problems in R.java were able to solve it by cleaning and rebuilding the solution, but this did not work for me. It will compile during the clean, but will fail to compile if I try to run it after that.
It means that at some XML (like layout/*.xml) you have tags with ids with those names android:id="#+id/1"
Since numbers are not a valid java identifier you must change the values in the XML to a alphanumeric string such 'a' ...
The class R is a compilation of the identifiers from all resources into res their names will be a variable into R, so name accordlyng
I got this error for wrong nameing:
<string name="1ab">text</string>
Resource name must begin with character and it worked
<string name="ab">text</string>
So just check if you have any resource name with wrong character in all your resource file(string.xml, style.xml, attrs.xml) and folder(drawable, layout).
Note: A variable's name can be any legal identifier — an unlimited-length
sequence of Unicode letters and digits, beginning with a letter, the
dollar sign "$", or the underscore character "_".
Thank you.
I coded an enum type which brings up the following Syntax errors when I run my created JUnit test for it:
java.lang.Error: Unresolved compilation problems:
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete EnumDeclaration
Syntax error, insert "}" to complete ClassBody
My enum type has static functions which for a particular String, returns an enum constant. Here is some of my code of the enum type:
public enum MusicType {
ACCIDENTAL, LETTER, OCTAVE, REST, DUR, CHORD, TUPLET;
public static MusicType is_accidental(String a){
if (a=="^" | a=="_"|a=="=")
return ACCIDENTAL;
else return null;
}
}
The rest of my static functions are very similar (i.e. is_letter, is_octave, etc.), although some use input.matches(regex) function instead of checking to see if an input it equals a particular string.
Here is the beginning of the JUnit test which tests the function dealing with the accidental constant:
public class MusicTypeTest {
#Test
public void accidentalTest(){
String sharp = "^";
String flat = "_";
String natural = "=";
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(sharp));
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(flat));
assertEquals(MusicType.ACCIDENTAL, MusicType.is_accidental(natural));
}
}
The other functions in my JUnit test which test all the enum static functions are coded similarly. I cannot figure out why I have these syntax errors (this is my first time coding an enum type). I've been coding in Eclipse and have not found any missing "}"s as of yet. I don't know if this has anything to do with the way I've written the test or the way I've declared my variables. Does anyone know why I have these syntax errors?
I had this same problem with Eclipse. It was a misleading syntax error message. It was due to a misplaced ";" after an annotation.
Double check your code ignoring the message.
I was getting this error while writing an Android app. All my brackets were closed; I was following an example from a different site. I ended up selecting the entire text for my code, cutting, saving, and pasting the code back. The error went away. It's very possible that Eclipse got stuck...
Both the enum type and the class that you have just posted have two opening braces ({) and only one closing brace (}). If I had to guess, I'd say you need to put one more closing brace at the end of each of these files.
I have an Java class with a static final method getAll:
public static final Vector<Category> getAll(Context context, ContentValues where) {
ArrayList<Integer> IDs = null;
if(where != null && where.containsKey(DatabaseAdapter.KEY_PRODUCT)) {
IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_PRODUCT_CATEGORY, new String[] { DatabaseAdapter.KEY_CATEGORY }, where);
} else {
IDs = OvertureItem.getAll(context, DatabaseAdapter.TABLE_CATEGORIES, where);
}
Vector<Category> categories = new Vector<Category>();
for(Integer id: IDs) {
categories.add(Category.get(context, id));
}
return categories;
}
Now I want to hand in null as a value for the where statemant so that it will just be ignored later on in the code. Anyway in the testcase for this method I have:
Vector<Category> categories = Category.getAll(context, null);
Which then in turn gives me a NoSuchMethodError. I don't know exactly why it does that. The only thing I could imagine is that the null I hand in would not match the signature of the above method. But how can I overcome this? I already thought of overloading. But this would just end in rewriting most of the code. At least when I do it, how I think.
Any suggestions on that?
Phil
P.S. This is the stack trace I get:
java.lang.NoSuchMethodError: com.sap.catalogue.model.Category.getAll
at com.sap.overture.test.model.CategoryTest.testGetAll(CategoryTest.java:59)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
If the method did not exist at compile-time, then the code would not compile.
If you get NoSuchMethodError at run-time, then this suggests that the version of the Category class you are running against is different than the version of the Category class you are compiling against.
What is your setup like - is this class in the same project? Are you copying in JARs from another project?
The real answer
So I now finally figured it out and it wasn't as obvious as I expected. I started wondering, when every new test case for any new method I wrote would give me the NoSuchMethodError. So I digged a little bit deeper and then, suddenly it came to my mind: "I changed the package name of the android application". I thought this would not make any difference to the test project as long as I kept the properties right in the AndroidManifest.xml but I was wrong!
In fact when your application package is named com.foo.bar.app, the package for your tests has to be named com.foo.bar.app.test! What happened was, that with my old configuration somehow the classes that sat in the bin/ folder were used. I thought, that they should have been deleted when I cleaned the project but they weren't. This way all of the older test cases would still pass and only the new ones would give me the NoSuchMethodError. After I deleted the bin/ folder manually I got a whole bunch of errors. I then renamed the package holding the test cases and did a full clean/ rebuild on the project et voilá everything is back to normal again.
Thanks for all the tips! I really appreciate your help that just kept me digging to the bottom of the problem. Hope this here will help anybody with the same problem in the future.
Phil