Aimkiller
Aimkiller

Reputation: 263

Android Studio project doesn't recognize some classes

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

Upvotes: 1

Views: 6413

Answers (5)

user2874349
user2874349

Reputation: 1

To resolve this import issue I had to create the classes in Android Studio and then copy and paste the old code into the new class with the same name.

Upvotes: 0

Miguel Tomás
Miguel Tomás

Reputation: 1911

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

Upvotes: 0

justcurious
justcurious

Reputation: 1

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.

Upvotes: 0

Mayur Dabhi
Mayur Dabhi

Reputation: 3926

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.

Upvotes: -1

Aimkiller
Aimkiller

Reputation: 263

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

Upvotes: 3

Related Questions