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) Improving Our Code Simple Refactoring: Creating a Class

The code fragment you pasted uses classes that are not accessible by imports in new context. Not sure what happened.

The above message appeared when I tried to paste the code from the FunFact to the FactBook. It wants me to select a class to import to the new file. They give the choice java.util.Random

6 Answers

Ratik Sharma
Ratik Sharma
32,885 Points

This could just be because of Auto Import in Android Studio off. Use this to enable it: http://stackoverflow.com/a/16616085 and hopefully things will import automatically when copy-pasting.

Slava Fleer
Slava Fleer
6,086 Points

thanks it's still not exactly like in video. it adding import line for random and not green in package. hope it is not important. would be nice to know why the difference ;)

I tried doing the same and it automatically places the import for me. Maybe some default setting for auto import might have been turned off.

Just check that from the link here IntelliJ Auto Import

Ratik Sharma
Ratik Sharma
32,885 Points

You need to import java.util.Random It's required for the project. If that's not your problem, please elaborate and I'll help you out!

During simple refactoring, you would have copied the section along with the Random declaration from FunFactsActivity file into a new file called FactBook. It was a simple copy paste procedure. However, when you did that and ran the program, the class FactBook lost track of Random class since it was not imported like it was in FunFactsActivity file. For Random to be used we have to implicitly import it. Rest Ratik's answer is completely justified else if its some other reason as pointed out.

I guess my big confusion is that when Ben copied and pasted the same code, he did not get this warning. I studied the video and did exactly the same thing. What would be the difference? Still do not understand. Just a note: this is my first ever app class.

When Ben copy pasted the code from one file to the other intelliJ (thus Android Studio) was smart enough to import the Random file itself.

Maybe its an IDE issue. Are you Eclipse or Android Studio?

Android Studio. It just happened again when pasting to the ColorWheel.

Goran Penov
Goran Penov
1,635 Points

I have the same problem and java.util.Random automatically placed the import for me. Still got the errors.

Ratik Sharma
Ratik Sharma
32,885 Points

Sometimes cleaning and rebuilding the project helps. Go to Build -> Rebuild project or Clean Project

I had this issue too, so I checked the forum to see if this was something I needed to correct before moving on. I took Ratik's suggestion to clean the project--and it appears that cleaning it deleted the FactBook class that we just created? I can't seem to find it. Rebuilding it didn't put it back, as far as I can see.