I've just recently become interested in programming, and I want to create Android apps for phones or tablets. I've come a long way in a couple weeks from knowing almost nothing about java/xml. I'm very serious about this. I'm going to find the answer to this question one way or the other. In fact, I hope to have it figured out before anyone answers this. I've fixed many issues without resorting to asking anyone, but I've just been stuck on this issue too long. I figured I'd give this a shot.
I'm using an older tutorial to build a practice twitter app (the tutorials for these seem to be everywhere, which is why I chose it). I'm using Eclipse for an editor.
The following is an example of code from the tutorial. which relates to my question:
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timeline);
Apparently since the intro of ADT 14, you can no longer use the (R.layout.timeline) phrase, which the error message refers to as a "switch statement."
Now, in a post I found on another site, someone who had a similar issue shows a screenshot of the "quick fix" in Eclipse using ctrl+1. in the screenshot, the fix that pops up says "convert switch to 'if-else' statement." This fix does not pop up in my version of Eclipse. My quick fix options are "migrate Android code", "create field 'timeline' in type 'layout'", "create constant 'timeline' in type 'layout'" or "rename in file."
If I choose "migrate Android code", a window pops up informing me of the ADT 14 update, and how switch statements are no longer allowed in library projects. It says to convert the switch statement to an "if-else" statement by pressing ctrl+1 for the quick fix, then choosing "switch to 'if-else' statement" like it does in the screenshot I found. But again, when I do this, that option does not pop up.
I would much rather know what needs to be changed in the code than know how to make the right quick fix pop up. If it isn't too much trouble, an explanation of why exactly these changes are affective would be very helpful. I have many (MANY) errors in my java files right now, but most of them are due to this exact problem in different forms. If I could see just one before/after example I could probably figure it out from there without an explanation. But after hours of searching, I cannot find that so far by googling.
Thanks so much in advance....
And for the record, I don't see any switch classes in any of my java files, if that makes a difference for the answer...
Somehow I overworked this error. I have deleted my project from workspace. Reverted it's .project files to old ones commited at SVN. Then at Eclipse uninstalling ADT and install it again. Then import my project. - > Libraries are added the old way and as result all fields at R.java are now final. Hope this helps.
I hope you got things working.
I agree that this sounds like some weird Eclipse error that is not what it seems, and not what Quick Fix says it is. Eclipse has been known to lie from time to time. Cleaning your project (as @Stephen Dubya said), cutting out the offending code, saving the file, pasting it back in, and saving again...these are some of the non-obvious tricks that sometimes get Eclipse to behave.
In general, I think it would be good for you to get more familiar with some of the Java basics like
switch
; I think it will make your learning of Android a lot easier. Although the tools and documentation keep improving, Android is still young and not always easy to learn using tutorials, especially when you aren't used to writing code at all or using complicated IDEs like Eclipse.But I digress. It is only in Android library projects, not regular projects, that Android doesn't treat resources as constants (
final
variables), since ADT 14. That means means that in library projects, you can't useR.layout.timeline
or similar resource variables in yourswitch
statements. You can onlyswitch
on whole numbers or enum values.Using
if-else
all the time may be your best bet, anyway.switch
can, especially for beginners, lead to logic errors in your code, and anything you can write usingswitch
can be rewritten usingif-else
blocks. Personally, I've stopped usingswitch
in Android and other Java code altogether.I've been having the same issues. For me, I was switching on view.getId(). Before the switch, declare int id = view.getId();. Then switch in id. Then you can ctrl+1 click and the "Convert Switch to If/Else" should pop up.
They made this change to decrease the build speed. My projects now build in roughly 1/10th of the time. I'm glad I upgraded the ADT.
Make sure you click on the
switch
keyword itself then press Ctrl + 1.This confused me at first as well...
If it still doesn't show up, what version of Eclipse are you using?
If you are using a Mac select the keyword
switch
and click Shift + Command + 1.That will show a prompt to change
switch
toif else
conditions.To me, it looks like you don't have a timeline resource.
Breaking down R.layout.timeline
R refers to Resources
layout refers to the collection of layouts in your application
and timeline refers the specific resource that you are trying to apply setContentView() too.
If my hunch is correct, you don't have the timeline resource. Create a new XML file in your layout folder in eclipse and name it 'timeline' That should resolve the issue.
You need to place the curse right before the s in the word switch, and the press cmd 1 . If you switch statements have fall-through clauses, the option to convert won't be available.