问题描述
请教各位一个问题,我想在游戏结束后显示最高得分,但是却失败了,请问下是在哪里出错Hunt_Midlet.javapublicintgetBestRecord(){returnhasBestRecord?bestRecord:-1;//返回最高成绩}publicbooleancheckBestRecord(intrecord){if(!hasBestRecord||(record>bestRecord)){hasBestRecord=true;bestRecord=record;writeRecordStore();returntrue;}else{returnfalse;}}privatevoidreadRecordStore(){hasBestRecord=false;RecordStorers=null;ByteArrayInputStreambais=null;DataInputStreamdis=null;try{rs=RecordStore.openRecordStore(RS_NAME,false);//如果不存在,新建byte[]data=rs.getRecord(1);bais=newByteArrayInputStream(data);dis=newDataInputStream(bais);bestRecord=dis.readInt();hasBestRecord=true;}catch(IOExceptione){}catch(RecordStoreExceptionex){}finally{if(dis!=null){try{dis.close();}catch(IOExceptione){}}if(bais!=null){try{bais.close();}catch(IOExceptione){}}if(rs!=null){try{rs.closeRecordStore();}catch(RecordStoreExceptione){}}}}privatevoidwriteRecordStore(){RecordStorers=null;ByteArrayOutputStreambaos=null;DataOutputStreamdos=null;try{rs=RecordStore.openRecordStore(RS_NAME,true);baos=newByteArrayOutputStream();dos=newDataOutputStream(baos);dos.writeInt(bestRecord);byte[]data=baos.toByteArray();if(rs.getNumRecords()==0){rs.addRecord(data,0,data.length);}else{rs.setRecord(1,data,0,data.length);}}catch(IOExceptione){}catch(RecordStoreExceptione){}finally{if(dos!=null){try{dos.close();}catch(IOExceptione){}}if(baos!=null){try{baos.close();}catch(IOExceptione){}}if(rs!=null){try{rs.closeRecordStore();}catch(RecordStoreExceptione){}}}}voidmenuListHighScore(){Display.getDisplay(this).setCurrent(newHighScoreScreen(this));}voidhighScoreBack(){Display.getDisplay(this).setCurrent(menuList);}-----------------------------------------------------------------------------------------------------HighScoreScreen.javapublicclassHighScoreScreenextendsFormimplementsCommandListener{privatefinalHunt_Midletmidlet;privatefinalCommandbackCommand;/**CreatesanewinstanceofHighScoreScreen*/publicHighScoreScreen(Hunt_Midletmidlet){super("最好成绩");this.midlet=midlet;intbestRecord=midlet.getBestRecord();Stringtext=(bestRecord==-1)?"目前还没有,期待你的表现哦!":(Integer.toString(bestRecord)+"分");append(newStringItem("最高分:",text));backCommand=newCommand("后退",Command.BACK,1);addCommand(backCommand);setCommandListener(this);}publicvoidcommandAction(Commandcmd,Displayabledisplay){midlet.highScoreBack();}}
解决方案
解决方案二:
报什么异常?
解决方案三:
还有你确定在你读readRecordStore()的时候,这个RecordStore-->RS_NAME已经存在了吗?如果不存在会有异常的。
解决方案四:
引用1楼zhouyongyang621的回复:
报什么异常?
没有异常,只是游戏结束后没有显示最高成绩的界面