Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio not recognizing import statements

I have a multitude of problems stemming from one central issue: Android Studio isn't recognizing imports.

For example, in my class "ActivityAbout" I have the following import statements:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

However, in the IDE (using the first import statement as an example), the import android.content. is appearing grayed out, and when I hover over it, it says "unused import statement" the Intent part is appearing red, and when hovering over that, it says "cannot resolve symbol Intent." This occurs for each of my import statements, with the first part being grayed out, and the last part being in red.

Now, this provides a multitude of issues in my code, as anything relating to those import statements appears red and when hovering over it, I get the message "cannot resolve symbol ____"

Here's the kicker- my app still compiles and runs upon opening it on my phone.

I suppose my main issue is that it is incredibly annoying, and auto imports doesn't work either. (I have checked off 'show import popup' in the settings, which is the answer I found for other people having issues with auto import not working). My other concern is that this would make my app ineligible for release on the play store. I'm still looking into the publishing process, so I don't know if that is actually an issue or not.

My question (if no one has an answer for how to fix this) is: is this a problem with the IDE itself or the code?

EDIT: added the other import statements

like image 590
Mairead Avatar asked Aug 31 '17 13:08

Mairead


People also ask

How do I change import settings in Android Studio?

Launch the Android Studio DMG file. Drag and drop Android Studio into the Applications folder, then launch Android Studio. Select whether you want to import previous Android Studio settings, then click OK.

What is r class in Android?

The R class gets code generated by the IDE and build tools based on our current roster of resources. Each resource gets a corresponding R value based on the type of resource ( R. layout , R.id , R. string , etc.) and the name of the resource ( R.


2 Answers

I just had this issue. I resolved it by clicking File > Invalidate Caches / Restart... > Invalidate and Restart. When Android Studio restarted and indexed all my code again, the issue was gone.

I have no idea what caused it...

like image 187
camerondm9 Avatar answered Oct 16 '22 13:10

camerondm9


I had a similar issue with my project. I resolved it by going to File > Sync with File System and then closing the project and importing it again

You may need to resync with the gradle again but hopefully it works for you

like image 20
King Keet Avatar answered Oct 16 '22 11:10

King Keet