3

I have just starting working my android studio project in another pc with same android studio version and it automatically makes used import statement as unused in the code and mark evey code related to particular import statement red. How can i fix this issue , its very annoying. but when i run the project ,it works fine without any error.

I have tried all those tricks including deleting .ide ,.gradle and even restarting with invalidate cash ,restard and disabling automatic import as well.

In the below screenshot , android studio just grayout all import statment i am using below and mark all those classes and its related method red in the code. how can i fix this issue

screenshot

tomtom
  • 345
  • 2
  • 15
  • What package is declared at the top of the file? The picture shows that imports for `MyXxx` classes are grayed out, but red classes are not `MyXxx` (e.g. `LogWriter` , `VideoWindow`, ...). What is the full class name for `LogWriter`? Is its import also grayed out? – Kuzneц Nov 08 '20 at 23:30

5 Answers5

3

MyAppObserver is unused import because you import it in the same file it's declared. As for others I am not sure because I can't see full file structure.

Nazarii Moshenskiy
  • 1,802
  • 2
  • 16
  • 35
  • @bdc Exactly the point, imports are unused because they are defined in same class.. and you won't get rid of them even if you restart studio, etc. That's by the standards of java. But if the class is called from other package, folder that imports should be used of course. – mmmatey Nov 10 '20 at 13:02
1

Your interface of the same name is causing a conflict.

interface MyAppObserver

Rename your interface and the imports should work correctly, and the red marks should go away.

Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28
  • no there is no error in code , it compiles without error. issue is some how its just makes all imported class as unused .. some issue with setting ... to remove those red marks ... – tomtom Oct 30 '20 at 15:21
  • I understand, but this is what's causing the issue in Android Studio. Renaming your interface would be an easy fix for this. However, if you're not able to do that, you can be explicit in which class/interface you're accessing by using the full class path. – Jens Zalzala Oct 30 '20 at 19:00
0

Try to clean then rebuild project. If this still exists then close Android studio then re-launch it again. sometimes I face the same situation but it got fixed after clean and rebuild

Ramy Ibrahim
  • 656
  • 4
  • 19
0

This happens sometimes. You will need to clean the project. If that doesn't work try invalidate caches/restart. First, go to File, then choose: Invalidate Caches/Restart...

1

Then this dialog pops up, choose Invalidate and Restart.

2

Most of the times when IDE doesn't behave as it should, you can try this.

Tugay
  • 2,057
  • 5
  • 17
  • 32
0

You don't need to import the interface as it is inside the same file.

Just click on the optimize imports

You can make a separate file for the interface.

enter image description here

enter image description here

Saif
  • 293
  • 2
  • 9