Android Studio “File was loaded in the wrong encoding:'UTF-8'” and “Java file does not declare any class” out of nowhere

↘锁芯ラ 提交于 2019-12-25 03:14:03

问题


Please note that I've read "File was loaded in the wrong encoding:'UTF-8' in IntelliJ IDEA" this, and everything else related to this issue that I'm having which is going to make me rewrite everything that I did in my project in past 2 days.

Now, the only reason I'm asking this question, is because if someone else faces this problem in future, it'd possibly have a solution by then?

So here is the problem:

I was working on my app which is already present on Play Store, I made all the changes in code, and wanted to add stuff in old java file. But, I was suddenly greeted with this beautiful thing:

Now, the error first said "File was loaded in the wrong encoding UTF-8" and gave me bunch of options to change my encoding. Now, as a programmer, I know if I have any issues, I should Google them, and found all the answers related to this issue, and I tried all those, fortunately, after trying all those answers, above error was gone, and only one "warning(?)" remained saying "Java file does not declare any class". Now, the amazing part is, if it was encoding error from Android Studios Code editor, it should have been totally fine if I opened it in another text editor right? I opened it using "Brackets" & also "Notepad", but I was again greeted with this beautiful thing:

Now, I'm totally clueless as to where I went wrong here, as some classes are working fine (most of those are Activities), but a lot of them which are not activities, are all gone and have become something like this. It's a nightmare and everything I wrote in past 2 days seems to have gone(because my last push was 2 days ago, I know I'm dumb). XML files are fine, and only Java files have this problem.

Things I tried:

  1. Deleting Android Studio settings from C:\Users\PersonWhoDidntPushChanges\.AndroidStudio3.4

  2. Reloading File encoding with UTF-16 and then again back to UTF-8. (Please note that only Reloading does not write the contents on disk and change the original encoding, there's a different option for that)

  3. Deleting .idea folder from the project, and reloading the project.

  4. Usual stuff, Invalidate Caches/Restart, Clean Project, Rebuilt Project.

Please also note that app ran fine and Android Studio did not give me any error until I actually opened this file, and when I saw this file in such format, I opened bunch of other classes to find those stopped working too. Any help regarding this matter would be appreciated.

I'm using

Android Studio 3.4.2
Build #AI-183.6156.11.34.5692245, built on June 27, 2019
JRE: 1.8.0_152-release-1343-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0

EDIT 1: Some XML files were also changed by Android Studio. The only solution that I found currently is just replacing the complete project with the one that I had on Github. Apparently some of the libraries have also changed (FloatingActionButton).

来源:https://stackoverflow.com/questions/56996857/android-studio-file-was-loaded-in-the-wrong-encodingutf-8-and-java-file-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!