Intellij Cannot resolve symbol on import

2019-01-12 23:49发布

This problem happens intermittently for different libraries and different projects. When trying to import a library, the package will be recognized, but the class name can't be resolved.

If on the import statement, I right-click -> Goto -> the package's declaration, I see all the decompiled classes displayed in the side pane -- Including the ones I need -- If I try to auto-complete the import statement, I notice the class I need is not featured in the dropdown.

I tried invalidating caches already, doesn't work. I cannot find any class conflicts -- there is no other jar file in my classpath with the same package name. I am able to import this class into other projects.

Please see screen shots:

enter image description here

enter image description here

Anyone have a clue?

22条回答
Evening l夕情丶
2楼-- · 2019-01-13 00:17

I found the following answer from @jossef-harush and @matt-leidholm useful from another link

  • in IntelliJ editor, click on the red keyword (Integer for example) and press ALT + ENTER (or click the light bulb icon)
  • select Setup JDK from the intentions menu

IntelliJ intentions menu

  • click on Configure

Project SDK selection dialog

  • In my case, the JDK path was incorrect (pointed on /opt/jdk1.7.0_51 instead of /opt/jdk1.7.0_65)

Broken Configure SDK dialog

  • click on the ... and browse to the right JDK path

Fixed Configure SDK dialog

  • let's clear the cache

IntelliJ File menu

查看更多
我命由我不由天
3楼-- · 2019-01-13 00:17

@Anton Dozortsev I was driven crazy by a similar behavior; I ended up re-installing the IDE a bunch of times, only getting past the issue after removing the IDEA app, all caches, preferences, etc.

I tried all kinds of steps in the interim, (like restarts, invalidations, deleting .idea and *.iml files, and others.)

Turns out, the problem was due to IntelliJ's idea.max.intellisense.filesize setting. I had set it to 100KB, which was smaller than my dependency's size, leading to IntelliJ showing it as unknown, just like in your screenshot.

Fix:

  1. Click on Help -> Edit Custom Properties

  2. Set the property to a larger value; the default is 2500KB idea.max.intellisense.filesize=2500

查看更多
劫难
4楼-- · 2019-01-13 00:18

Check your module dependencies.

  1. Project Structure (Ctrl+Alt+Shift+S).
  2. Modules
  3. Select your problem module.
  4. Change tab on top of window "Dependencies".
  5. Check what needed library (maybe, you need to add specified library in the tab 'libraries') or module has listed here and it has right scope ('complile' mostly).
查看更多
【Aperson】
5楼-- · 2019-01-13 00:18

What did it for me is to edit the package file in the .idea folder as I accidentally added sources to this jar library and android couldn't resolve it by deleting the sources line as marked in the b/m picture library error.

Then rebuild the gradle and bam problem solved.

查看更多
走好不送
6楼-- · 2019-01-13 00:19

There can be multiple reasons for this. In my case it was wrong source root issue. Invalidate caches didn't work along with other solutions.

Check your module source roots.

  1. Project Structure (Ctrl+Alt+Shift+S).

  2. Modules

  3. Select your problem module.

  4. Change tab on top of window "Sources".

  5. Remove unwanted source roots. Keep one and add src and test source roots in this root.

查看更多
仙女界的扛把子
7楼-- · 2019-01-13 00:20

File -> Invalidate Caches/Restart or rebuilding the project did not work wor me.

What worked for my Gradle project was to "Refresh all Gradle projects" from the Gradle tab on top-right corner of IntelliJ v2017, using the yellow marked button shown below:

enter image description here

查看更多
登录 后发表回答