1

Recently my project got messed up. I don't know how it became like this. There was no power failure or anything happened while using Android Studio. Next day I opened my project and now it's a full mess. I attached the images of how they look now.

Classes are not recognized as you can see they have .java extension showing and when I opened one of them it show like below. but if I opened the file directly from notepad or something the source-code of these files are not changed.

I did delete .idea all .iml files inside the project then invalidate & restart build project again. But no luck, no matter what I did it doesn't get resolved. I have an old backup of this project and not a recent one, so I want to get this fixed .

I tried opening my other projects and they work very well. The problem seems to be only in this project. Android Studio is updated to last version before this problem persisted. So it's not also a bug in Android Studio.

ImageOne

ImageTwo

This is how the Java file looks when I opened with Notepad++. The code didn't disappear, no I also didn't write xml code in my Java file:

ImageThree

halfer
  • 19,824
  • 17
  • 99
  • 186
Aimkiller
  • 263
  • 1
  • 5
  • 13
  • 1
    the first image named java class, but the code is in xml – John Joe Dec 07 '18 at 06:01
  • Like john's answer, you write xml code in Java class – Pandu Abbiyuarsyah Dec 07 '18 at 06:33
  • have a look at [this](https://stackoverflow.com/questions/53496043/trying-to-open-java-and-xml-files-in-android-studio-321-it-shows-some-error) and [this.](https://stackoverflow.com/questions/30012529/android-studio-1-2-project-encoding-mismatches-by-default) – Rumit Patel Dec 07 '18 at 07:04
  • why the downvote? @PanduAbbiyuarsyah I didn't man. My project was working very well. after the next day this is what I get. can't you understand what I meant? The first image is in xml, but that isn't I coded. It was changed after the restart – Aimkiller Dec 07 '18 at 08:29
  • @JohnJoe man I didn't write these xml. my project was working very well. then the next day this problem arised. those files with extension .java was classes and were not any xml code in there, next day when I opened the project xml code was there my previous code that were in the class disappeared – Aimkiller Dec 07 '18 at 08:31
  • @JohnJoe see the last image I attached – Aimkiller Dec 07 '18 at 08:35
  • @PanduAbbiyuarsyah see the last image I attached – Aimkiller Dec 07 '18 at 08:35
  • It sounds like you only have irregular backups to protect you. If you are not using it, version control is recommended, and pretty much essential. – halfer Dec 07 '18 at 18:10

4 Answers4

3

I just had to copy all the source code in .java files back into android studio editor, which the files are with .java extension. Problem solved

Aimkiller
  • 263
  • 1
  • 5
  • 13
0

I had a similar problem, but with Kotlin files.

What worked for me was shortening the file name by a few characters (but not the class name).

I suspect that there's a limit to the length of the path which can get quite long if you try to organise a large(ish) project using subfolders.

0

If you have just created a new package directory inside your project and moved class files from the project into the package directory you just created you will get this error.

To avoid it. Instead of moving class files, copy them and next build your project. Very important this part to build the project.

Next you need to delete the duplicate class files outside the package (Can't use safe delete for this)

And finally you need to change the imports on all affected class files to reflect the new namespace / location. My suggestion is to use "replace in path" from the Menu Edit > Find

Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23
-1

I have same problem occur few days ago, solved by below steps

  1. uninstall android studio,

  2. download latest from link

  3. delete all file from default android studio folder (C:\Program Files\Android\Android Studio)

  4. install new downloaded android studio , all worked well.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25