I just started to learn J2ME and right from the bat I got this error when using Random class. Arg, it's so frustrating. Anyone got the same problem before? 开发者_JAVA技巧I already tried to restart Eclipse, write only the code for the random generator to isolate it but to no avail. I'm using CLDC 1.1 and MIDP 2.1 by the way.
Seems you are pointing out the wrong CLDC and MIDP libraries. new Random().nextInt(x) exists in CLDC 1.1.
If you are using CLDC 1.0 you can create your own implementation of nextInt(int):
public static int random(Random r, int n) {
if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)r.nextInt()) >> 31);
int bits, val;
do {
bits = r.nextInt();
val = bits % n;
} while(bits - val + (n-1) < 0);
return val;
}
(Same implementation as nextInt(int) in the CLDC 1.1.)
nextInt(int) is only available since CLDC 1.1, so you have to specify CLDC 1.1 instead of 1.0
if you specified cldc 1.1 an still get this error its most likely an error of eclipse. took me hours to find: in Eclipse go to Windows -> Preferences -> Java ME -> Device Management -> choose your Default Device -> Edit -> Libraries -> There you have to remove the cldc_1.0.jar
I had the same problem in netbeans, and solved it.
- Right click on your project, go to properties.
- Go To Libraries & Resources under build
- Add Library
- scroll for JMUnit for CLDC11
- click OK and build. all problems solved.
I just left JMUnit for CLDC11 there. not really sure if I need it. maybe I'll test without it once I have made up for lost time.
精彩评论