• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Syntax error on tokens, AnnotationName expected instead

 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm new and I hope that I will be useful here. I have a problem with my code. It gives me the error I put as title:


This error is in the first line and I have also one in the last, where eclipse looking for a @ (O.o).
Also I will insert the variable choice1.

final Button choice1 = (Button) findViewById(R.id.A);
Hope I can help you. Thanks guys
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't look like a working example. Where is backgroundA defined and initialized?
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please post the exact text (←link) of the error message. Then we can see what is going wrong. I have just updated that link to add instructions for Linux users.
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all,
you where right, I'm sorry. I forget a variable. So backgroundA is final defined as
As error I don't know how to implement it as I have a mac
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something is odd here:



What is the type of View? Is it javax.swing.text.View? Methods should be named in camelCase (first letter lower case, capitalize the first letter of each word). Should OnClickListener() be onClickListener()?
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I put all the code so you don't have other problems:

[Edited - fixed indentation]

Then I have other buttons but the code is almost the same and also the errors
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this an Android app? Do you have "import android.view.View.OnClickListener;" in your program? You might want to reload your Android jars. Also, post the full error text.
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using eclipse so the import are automatic. What does "post the full error text" means? If I'm going on the line that has the error it says "Syntax error on tokens, AnnotationName expected instead" and I'm using a Mac
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Eclipse, Android project. These are good things to say up front. So Eclipse is getting a compiler error. Have you tried putting @Override before public void onClick(View view) ?
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing change
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm out of my depth. You might want to try posting to http://www.coderanch.com/forums/f-2/GUI.
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say t change directory of the site? Ok I will try there
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A man close my post because says that is duplicate. SOMEONE HELP ME PLEASEEEE
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't duplicate the post - we can move a thread to another forum if it's more appropriate.

However looking at your full listing - that whole for loop should be in a method, but it isn't. You've got it directly in the class body. You can have declarations there, but not statements like that. Maybe you intended it to be in a constructor?

Edit: Oh, and be careful throwing around insults like "stupid", please? Thanks.
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then do it. I don't know how to do
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok sorry but are three days that I'm working on this piece of code. How can I change it, in your opinion? The if statement needs to disable the button after two click. Is there a different way to do it?
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more shot in the dark: have you tried Alt-F5? If this is a maven project, it will update the maven dependencies.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't know how to write a constructor? I'd suggest you need to go through some of the introductory tutorials - see http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html for example. You really need to know the language basics before trying things like graphical user interfaces. Briefly, though, a constructor is a block of code that is run to initialise the state whenever a new object is created. A simple constructor looks like this:

It looks like a method, except there is no return type, and it has the same name as the class. You can also have constructors that take arguments, and you can have overloaded constructors, but this simple example should be enough for you to be able to move your initialisation code into one.

Edit - sorry, I may have been mislead by the inconsistent indentation, that made it look like line 37 onwards was at a different level of indentation. The error message you're getting - which line are you getting it at?
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what is a constructor What I mean was: Should I have just to add a constructor and then put everything in?
Knute I'm using eclipse. I don't thing it will work
 
Knute Snortum
Bartender
Pie
Posts: 2890
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try right-clicking on the project in the project tree (usually to the left). Then click Maven, then Update Maven Project (or something like that). The hot key should be Alt-F5. I use this all the time in Eclipse.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough . As I edited above, sorry, I got the wrong end of the stick there.

By the way, I'll copy this thread into the forum you tried to start a new thread in, so that it's visible there.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason I made that mistake is that an error like that suggests that the compiler thinks it's in a class body when you intend something to be in a method, or something like that. I've fixed the indentation in your earlier post, and I'm wondering what the next few lines are, because the problem might be there. I've seen similar errors when using an anonymous class like that if it isn't ended correctly.
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks so much. Hope someone could help me. I'm desperate. I try to insert the constructor but nothing. Eclipse says to delete it
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Try right-clicking on the project in the project tree (usually to the left). Then click Maven, then Update Maven Project (or something like that). The hot key should be Alt-F5. I use this all the time in Eclipse.

There is no "Maven" in eclipse
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AnnaMaria Accardo wrote:Ok, thanks so much. Hope someone could help me. I'm desperate. I try to insert the constructor but nothing. Eclipse says to delete it


Forget the contructor - that was a red herring. But show us the next few lines of the code. At least until the end of the if statement that starts on line 52 (even better until the end of the for loop that starts on line 50, unless that's really long).

One thing I noticed while fixing the indentation is that the if statement doesn't have a following brace. That isn't necessarily the cause of the error - we can't see enough to tell - but it's much better practice to include it as it can lead to non-obvious errors.
 
AnnaMaria Accardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, ok. I correct all the problems but the code shows one card and then stops. I can click the button ones and not twice. What's going on? In the first case (button A) it works but then it just show one card and not two

The codes are the same. I don't understand why for the first works and for the others not
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you provide the layout where all these buttons exist so we can tell what the difference between the buttons whose OnClickListeners you are assigning?

Also, if you can explain in more detail exactly what actions you are taking, what you expect to happen and what is happening, that would help us help you. Also, add in logging statements and use LogCat to follow precisely what methods and lines of code get executed when. That way you will know the flow of control and can determine the why and how, then determine how to redirect the flow the way you intend it to go.

A few comments on your code:
- It is really hard to read your code because you don't indent consistently. It is nearly impossible to tell which lines are part of which method, anonymous inner class, if condition, for loop, etc... because you don't indent consistently. If you end up indenting too deeply that you feel your lines of code are too short, then chances are you are doing too much in one method - break the code out into multiple methods and it becomes a lot easier to read

- Good naming also goes a long way in making your code readable. There are a lot of 'A's and 'B's, and v1, and linearlayout1... Try to rename the variables more consistently with what they represent rather than the type.

- You have a lot of confusing code.
-- -- In line 21 you find a view, but never assign it to anything. If you don't need the return value, get rid of the method call.
-- -- In line 24 you get the R.array.DeckDraw as an int[], but in line 30 you get the same thing as a TypedArray. Why do you need to get it twice?
-- -- In line 26 you create a HashMap named DECK and on line 30 you create a TypedArray named Deck. Why two variables with the same name, essentially? Which do you use? Do you need them both? Can you be more specific as to what each one is? Example maybe the HashMap is cardNameToIdMap and the TypedArray cardIds or something like that?
-- -- In line 33 you define the variable v1, but it is only used inside the for loop. It is best to define any variable within the smallest possible scope. So you can probably get rid of line 33 and change line 45 to View v1 = //.... Of course, a better name than v1 would be best.
-- -- on lines 7 and 8 you create 2 final ints with a value of 0. IF you actually need these two ints, since they are final and you know the value at compile time, you should make them constants (static final): static final int RANDOMINT1 = 0; However, since their value is known (0) and constant then they aren't really random, are they? Maybe a better name is needed, of if they aren't used then get rid of them.
-- -- On lines 41 and 42 you create two Random generators, but I only see you use one. Are they both necessary? What does having 2 get you that having one does not? I would get rid of the second one as being redundant.
-- -- On lines 3 - 6 you create a bunch of Views named buttonA through buttonD. I don't see where you use them. They are also very easy to confuse with the Buttons named backgroundA through backgroundE found later in the code. Are these declarations (lines 3-6) necessary, do they need to be in the class scope? I would worry about this because in Android the Activity and view can be destroyed and remade at various points and by assigning views as instance variables you could be causing a memory leak. If you don't need to store those variables in the instance scope I would suggest not doing it.

For ease of reading and troubleshooting, I would try to bring the click listeners added to the background buttons out as new classes. I made two of them, one that does the assignment of the background, and one that does the disabling since not all of the buttons get disabled:

Then, using those classes and some of the things I described above the activity becomes:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic