[/code[code=Java]
下面是小弟用J2ME写的一个通讯录的小程序,到时运行的时候抛出安全异常,请问怎么解决啊?
PhoneMIDlet.java
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class PhoneMIDlet extends MIDlet implements CommandListener {
private Display dis;
private Form ff = new Form("欢迎!");
private String interfaceName;
// PhoneList pl = new PhoneList(); // 在此声明会出现会出现安全异常
// AddPhone ap = new AddPhone(); // ?????????
private Command ExitComm = new Command("EXIT", Command.EXIT, 1);
private Command phoneComm = new Command("电话本", Command.SCREEN, 1);
public PhoneMIDlet() {
dis = Display.getDisplay(this);
ff.addCommand(ExitComm);
ff.addCommand(phoneComm);
ff.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
dis.setCurrent(ff);
}
public void commandAction(Command arg0, Displayable arg1) {
if (arg0 == ExitComm) {
notifyDestroyed();
}
if (arg0 == phoneComm) {
// PhoneList pl = new PhoneList();
this.changInterface("PhontList");
}
}
protected void changInterface(String interfaceName) {
PhoneList pl = new PhoneList();
if (interfaceName.equals("FistForm")) {
dis.setCurrent(ff);
}
if (interfaceName.equals("PhontList")) {
dis.setCurrent(pl);
System.out.println("bbb");
pl.deleteAll();
pl.loadPhone();
System.out.println("ccc");
}
if (interfaceName.equals("AddPhone")) {
AddPhone ap = new AddPhone();
dis.setCurrent(ap);
}
}
public String getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
}
PhoneList.java
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
public class PhoneList extends List implements CommandListener {
Command backComm = new Command("BACK", Command.BACK, 1);
Command addComm = new Command("ADD", Command.SCREEN, 1);
Command delComm = new Command("DEL", Command.SCREEN, 1);
// PhoneMIDlet pm=new PhoneMIDlet();
// AddPhone ap = new AddPhone();
public PhoneList() {
super("电话本",List.IMPLICIT);
this.addCommand(backComm);
this.addCommand(addComm);
this.addCommand(delComm);
this.setCommandListener(this);
}
public void commandAction(Command arg0, Displayable arg1) {
PhoneMIDlet pm = new PhoneMIDlet();
// System.out.println("fff");
if (arg0 == backComm) {
pm.changInterface("FistForm");
}
if (arg0 == addComm) {
pm.changInterface("AddPhone");
}
if (arg0 == delComm) {
this.del();
pm.changInterface("PhontList");
}
}
public void del() {
PhoneOPE po = new PhoneOPE("PS");
po.openRecordStor();
po.delPhone(this.getString(this.getSelectedIndex()));
po.closRecordStore();
}
public void loadPhone() {
PhoneOPE po = new PhoneOPE("PS");
po.openRecordStor();
Vector vc = po.getAllPhone();
for (int i = 0; i < vc.size(); i++) {
this.append((String)vc.elementAt(i),null);
}
po.closRecordStore();
}
}
AddPhone.java
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
public class AddPhone extends Form implements CommandListener{
// PhoneMIDlet pm=new PhoneMIDlet();
// PhoneList pl=new PhoneList();
Command OKcomm=new Command("OK", Command.SCREEN, 1);
Command BACKcomm=new Command("BACK", Command.BACK, 1);
TextField userName=new TextField("用户名", "", 8, TextField.ANY);
TextField userPhone=new TextField("电话号码", "", 8, TextField.ANY);
public AddPhone(){
super("添加电话");
this.addCommand(OKcomm);
this.addCommand(BACKcomm);
this.append(userName);
this.append(userPhone);
this.setCommandListener(this);
}
public void commandAction(Command arg0, Displayable arg1) {
// PhoneList pl=new PhoneList();
PhoneMIDlet pm=new PhoneMIDlet();
if(arg0==OKcomm){
PhoneOPE po=new PhoneOPE("PS");
po.openRecordStor();
po.addPhone(userName.getString(), userPhone.getString());
po.closRecordStore();
pm.changInterface("PhontList");
}
if(arg0==BACKcomm){
pm.changInterface("PhontList");
}
}
}
PhoneOPE.java
import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
public class PhoneOPE {
String storeName;
RecordStore rs;
public PhoneOPE(String storeName){
this.storeName=storeName;
}
public void openRecordStor(){
try {
rs=RecordStore.openRecordStore(storeName, true);
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addPhone(String name,String phone ){
String str=name+":"+phone;
byte[] b=str.getBytes();
try {
rs.addRecord(b, 0, b.length);
} catch(Exception ex){}
}
public void delPhone(String str){
int lastID=0;
try {
lastID=rs.getNextRecordID();
} catch(Exception ex){}
for(int i=1;i<lastID;i++){
try{
byte[] b=rs.getRecord(i);
String strRec=new String(b);
if(str.equals(strRec)){
rs.deleteRecord(i);
}
}
catch(Exception ex){}
}
}
public Vector getAllPhone(){
Vector vc=new Vector();
try{
RecordEnumeration re=rs.enumerateRecords(null, null, false);
while(re.hasNextElement()){
vc.addElement(new String(re.nextRecord()));
}
}
catch(Exception ex){}
return vc;
}
public void closRecordStore(){
try{
rs.closeRecordStore();
}catch(Exception ex){}
}
}