Basic Java/Eclipse problem: standard libraries cannot be imported in Android project
I'm new to Java/Eclipse/Android, so this is probably an easy (if not stupid) question:
After creating a new Android project, I want to import some (what I think are standard) java libraries.
However, some import statements throw an error:
HelloAndroid.java:
import java.awt.Color;
(The import java.awt.Color cannot be resolved)
import javax.imageio.ImageIO;
(The 开发者_开发百科import javax.imageio cannot be resolved)
while others don't:
import java.io.File;
(no error)
I've done some googling and tried to add these classes to my classpath by going to project->properties->libraries but I haven't been able to figure out what to do exactly.
Any pointers are greatly appreciated!
Cheers,
Martin
Android provides its own API and only a subset of the standard Java API.
awt
and imageio
are not included in the Android API, so you can't use it.
For example, if you want to manipulate images, use classes from the android.graphics package.
Although Android apps are written in Java, they're not executed on the JVM, but instead a smaller virtual machine called Dalvik. This machine only supports a subset of the classes supported by the standard Java VM, so it's likely that those classes are not part of that support-set.
In general:
When you're not sure where the classes that you want are located (i.e., what package they're in), one method of discovering it is to simply not try to import them, but instead just use them in your code. Most IDEs will highlight your code to tell you that you need to import that class before using it, and they'll either a) add the import statement if there's only 1 class with that name, or b) provide a list of options containing various packages that all have that class in it, from which you can choose which you want.
EDIT: Technically, the language is not Java. Check out the comment below.
As other people have indicated, not all Java classes are present in Android. If you want to check if a particular class is present, the easiest thing to do is to check the reference docs.
精彩评论