Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 308
Hoff Avatar asked Mar 07 '10 17:03

Hoff


2 Answers

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.

like image 85
Desintegr Avatar answered Oct 29 '22 21:10

Desintegr


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.

like image 40
Nate W. Avatar answered Oct 29 '22 22:10

Nate W.