Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Basic Android Programming Generating a Random Number

Michael LaCroix
Michael LaCroix
5,828 Points

Java import issues in Android SDK

I've had an error pop up a few times concerning Java imports when I copy and paste a chunk of code into a new class (usually one we've created). It seems to resolve itself when I click import but I'd like to know why I have this error and he doesn't. Error posted below -

The code fragment which you have pasted uses classes that are not accessible by imports in the new context. Select classes that you want to import to the new file.

- java.util.Random

Peter Gordon
Peter Gordon
2,473 Points

I believe that's to do with auto-import and you can change to automatically include the import statements.

If you go to File > Settings then search for "auto import" you'll see a way to change the behaviour. You can manually navigate there from File > Settings > Editor > Auto Import.

It should be possible to change "Insert imports on paste" to always in order to never see this pop up again.

2 Answers

J.D. Sandifer
J.D. Sandifer
18,813 Points

I wanted to mention that this error is really just Android Studio taking care of things for you. I actually like knowing what it's doing for me so I realize what I'd need to do if I were coding from scratch in a text editor.

If you prefer not to see the messages, though, Peter's solution should take care of it for you:

"If you go to File > Settings then search for "auto import" you'll see a way to change the behaviour. You can manually navigate there from File > Settings > Editor > Auto Import.

It should be possible to change "Insert imports on paste" to always in order to never see this pop up again."

Salomon Alcoba Trujillo
PLUS
Salomon Alcoba Trujillo
Courses Plus Student 1,056 Points

i have the same issue, i get the error i do this "alt + enter" and choose import class, i have many other options, but i need to know why and what this solutions do