最近写了一个j2me的程序,在sun的wtk中运行,build 时没有任何错误,
但在run时却抛出这个java.lang.IllegalAccessException,这是为什么?
源代码如下:
package com.mot.j2me.midlets.ocrtest;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
class ocrtest extends MIDlet implements CommandListener
{
Display display = Display.getDisplay(this);
public Command okCommand = new Command("Yes" , Command.OK,0),
exitCommand = new Command("Leave", Command.EXIT,1);
public List optionList ;
public ocrtest()
{
optionList = new List("Begin work ? " , List.IMPLICIT);
optionList.addCommand(okCommand);
optionList.addCommand(exitCommand);
optionList.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException
{
display.setCurrent(optionList);
}
protected void pauseApp(){}
protected void destroyApp(boolean unconditional){}
public void commandAction(Command c , Displayable d)
{
if( d == optionList )
{
if ( c == exitCommand )
{
notifyDestroyed();
}else
{
//System.out.println("succeed!!!");
}
}
}
}
------解决方法--------------------------------------------------------
optionList.setCommandListener(this);
把这句放到public void startApp() {}中。