跪求J2ME高手解惑~~~~~~~~~~~~,分数倾囊相送~~~~~~~~~~~~

Jony-Li 2011-03-15 03:24:49
[/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){}
}
}










...全文
256 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
kf156 2011-03-21
  • 打赏
  • 举报
回复
PhoneMIDlet pm = new PhoneMIDlet();
是这句的问题,MIDlet不允许有多个实例,建议用静态方法或将PhoneMIDlet自身作为参数传递给其他类
AAAAAA01 2011-03-20
  • 打赏
  • 举报
回复
对我来说楼上的都是高手
scliuqiang 2011-03-18
  • 打赏
  • 举报
回复
你应该读取了手机联系人列表吧
在JAD配置文件中加上以下权限试试
MIDlet-Permissions:
javax.microedition.pim.ContactList.read,
javax.microedition.pim.ContactList.write,
OCodeBNothing 2011-03-18
  • 打赏
  • 举报
回复
每天接分路过
huang_63188 2011-03-18
  • 打赏
  • 举报
回复
在jad的文件Opthional Midlet Permissions属性里添加:microedition.io.Connector.file.read,javax.microedition.io.Connector.file.write
mopao001 2011-03-18
  • 打赏
  • 举报
回复
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");
}
}

在这里报的异常~~~如果我没看错的话
PhoneMIDlet pm = new PhoneMIDlet();
这句去掉应该就不会报了。。。你试试,应该这句是不允许的,我开始以为你要读取本地文件,代码太长了没有细看
Jony-Li 2011-03-16
  • 打赏
  • 举报
回复
万分感谢!!!!!!!!!!!!!
Jony-Li 2011-03-16
  • 打赏
  • 举报
回复
坐等各位高手解答~~~~~~~~~~~~~~~~~~~·
Jony-Li 2011-03-16
  • 打赏
  • 举报
回复
我将模拟器里面的的Security 属性改为了MSA maxinum 但是还是出现上面的那个错误,各位高手能不能详细告诉我一下 怎样就软件签名啊,截图给我看看嘛,谢谢各位了~~~~~~~~
Dongo2 2011-03-16
  • 打赏
  • 举报
回复
maxinum 调整可以到sun 的模拟器的安装下的偏好进行调整,
方法一:具体的是在 程序--》sun***--》偏好--》性能设置(修改里面的值)
方法二:如果你集成了IDE的话就可以直接在IDE去设置,方法类似

----如果还不知道的话可以找我
Jony-Li 2011-03-15
  • 打赏
  • 举报
回复
请高手救急啊~~~~~~~~~~~~~~
Jony-Li 2011-03-15
  • 打赏
  • 举报
回复
我编写的通讯录没有涉及到网络连接啊,怎么还需要软件签名啊~~~~~~~~~~~~~
mopao001 2011-03-15
  • 打赏
  • 举报
回复
说起来有些麻烦,你手头如果有j2me的API手册,里面有告诉你如何签名的,如果没有,就GOOGLE吧
Jony-Li 2011-03-15
  • 打赏
  • 举报
回复
高手解决啊~~~~~~~~~~~~~~~~
Wang-Xian 2011-03-15
  • 打赏
  • 举报
回复
坐等高手..........
Jony-Li 2011-03-15
  • 打赏
  • 举报
回复
怎么我将maxinum调到最大了,还是原来错误啊,还有就是程序签名怎么实现啊?
Jony-Li 2011-03-15
  • 打赏
  • 举报
回复
请问楼上怎么程序签名啊~~~~~~~~~~~~~~
车把式 2011-03-15
  • 打赏
  • 举报
回复
可以将wtk模拟器安全调至maximum
mopao001 2011-03-15
  • 打赏
  • 举报
回复
将程序签名之后就可以了,未签名的程序访问敏感API 就会报这个错误
凡员外 2011-03-15
  • 打赏
  • 举报
回复
访问本地文件了?security。。
加载更多回复(3)

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧