Switch.java has errors in android studio - java

I installed android studio in ubuntu recently and I'm pretty new to linux. I built an app that uses a switch widget. The problem is, when I tried to access the switch object using
Switch switch = (Switch) findViewByID(R.id.switch1);
I got an error saying that it is not a valid statement along with multiple errors like '(' expected, right after the the object name, the second word in the line, 'switch'
Out of curiosity, I went to Switch.java by ctrl clicking the class name. And over there I found over 72 errors.
This is what it looks like:
I have downloaded the latest google APIs after seeing the response to this question. And I still don't know how to rectify it. I am asking this question here as a last resort. It most probably will be some silly mistake on my part while configuring the install. Please help me!

switch is a Java language keyword so you can't use it as a variable name.

Related

Android Studio 3.3 Java 8 debugger "this" is not available in lambdas and cannot stop on "if"

I have changed my Android Java version to 8 wanting to simplify some code writing. An issue I am having, and I cannot figure out, is that I am getting a 'this' is not available in the debugger for the class properties when they are inside a lambda expression. As mentioned in the title I have updated to the latest Android Studio 3.3.
I have been searching around for some time now trying to figure out what I am doing wrong but with no luck.
Another issue that I am having is that when in lambdas (and anonymous in general) the debugger cannot stop on if statements (visually the breakpoint red circle never gets a tick mark), something I am pretty sure I could do before. Are there changes (or settings) in Android Studio 3.3 that caused this? What am I missing?
Any ideas/solution would be much appreciated.
EDIT:
There is another question concerning the if breakpoints issue specifically. The OP there gives a very generic answer that I am not satisfied with and it doesn't answer the why.
What I noticed is that the if statements that have complicated logic in them do not get active breakpoints. So an if (object.getSomething() == something) will not work, but if you assign that to a value and then use the value in the comparison it works just fine... mystery deepens.

Android Studio cannot resolve symbol text turns red

I know that if you write something wrong in code you get a red underline.
My problem is that the whole word turns red and I get the error
"Cannot resolve symbol '(and then whatever the code is)'"
when I hold the mouse over it.
I have downloaded Java just to see if it were the problem. I also followed this tutorial on how to download Android studio.
The program I made is from this tutorial on Youtube.
The path text in the top gets a red underline.
I don't know if it helps but i'm using Windows 10 and Android Studio 3.3
Here you can see my code turning red:
You're writing Kotlin in a Java file. Because of this, your IDE doesn't understand the symbols you're using to define variables. Note that even more recent versions of Java don't support val, which is why I presume you're attempting to use Kotlin here.
Your choice: write your code in either Kotlin or Java, but not both at the same time.

Android Studio not recognizing String

I've encountered a weird glitch in Android Studio:
As you can see it thinks that it is an error:
'class' or 'interface' expected
But it runs and build just fine, so is this just a visual glitch or can it have any effect during compiling?
I'm running Android Studio 0.5.8 for Mac
Things I've tried so far:
Restart Android Studio
Restart MacBook
Invalidate caches and restart
tried other strings (like "en-US" this result in the same effect)
If I use a string through the resources the glitch isn't visible (which it will be eventually, I'm just curious why this is happening)
IntelliJ and Android Studio have the ability to take string parameters and treat the strings as being in another language. For example, if you have a method that takes a string that's HTML, if you tell the IDE about it, it can give you syntax highlighting and content assist with the HTML in that string.
I think you've inadvertently set this string as being treated as Java, and it's giving you the Java syntax errors on it. You need to un-set that setting. Select the text, bring up the Search Action feature (on Mac it's ⌘ shift a). In the search box that comes up, search for Un-inject Language/Reference, as shown in the screenshot below:
try this
httpReqquest.addHeader("Accept-Language","en-US");
Since Android Studio is a bit raw, you can face the "glitches" in many different ways. They're annoying but don't affect the result, as you said. However, I personally don't think that we should endure the inconveniences (even if the Studio costs nothing).
I see a couple of solutions:
Continue with AS and ignore these weird things, hoping they will release a robust software once
Or, to migrate to IntelliJ IDEA as I did. The procedure is pricey but it's worth it.

changing switches to "if-else" statements

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...
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 to if else conditions.
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.
The issue happens because since ADT 14 resource identifiers are no longer final.
The quick solution you can change switch statements with if-else statements.
With Android Studio 4.1.3
Step:1. Just right click on the switch keyword of statement.
Step:2 Click on the first option Show Context Actions.
Step:3 Click replace switch with if.
Done
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.
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 use R.layout.timeline or similar resource variables in your switch statements. You can only switch 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 using switch can be rewritten using if-else blocks. Personally, I've stopped using switch in Android and other Java code altogether.
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.
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.

Android Error tracing in Eclipse?

I am developing Android app in eclipse with Android SDK 2.3, but i am unable to see the errors like we see in java or .net, there are no run time errors tracing, if there is some error i have to place alerts there to check it, where as compiler does not place any error in its window. Its quite strange that it gives compile time error but no run time error. Please help.
Thanks
Atif
Go to Window->Show View->Other.., and select Android->LogCat. This is the running system trace. I believe this is what you are looking for.
So first thing about android is, you will not be able to look your output details in console as your java or .net shows. As you might know emulator is what is considered to be your output window. And in order to find compile time errors eclipse makes it easy by highlighting your mistakes instantly. And as you have asked for, to look at your runtime errors android uses a special view called Logcat. This is were you will be listed out with all the runtime details of android. And as you have said that you are not able to view even System.out.println messages not being viewed, you might find it a bit strange but actually android does print these mesages in Logcat for you. So as the user sparkymat has suggested follow the steps and you are done.

Categories

Resources