问题描述
importjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;publicclassteextendsMIDletimplementsCommandListener{privateFormform;privateTextFieldtf,tf2;privateDisplaydisplay;privateStringItemsi;privateMCanvasca;publicCommandok;publicCommandexit;publicintx=0,y=0,wait=0,ind=0;publicString[]showstr=null,xystr=null;publicStringtext="若人生只如初见|何事秋风悲画扇|等闲变却故人心|却道故人心亦变|";publicte(){form=newForm("测试");tf=newTextField("请输入坐标:","10|100|500|",13,TextField.ANY);tf2=newTextField("请输入显示的文字:",text,50,TextField.ANY);si=newStringItem("提示:","坐标请以“X坐标|Y坐标|刷新时间|”格式。n文字请以“|”为句与句之间的分隔,且以“|”结尾。");exit=newCommand("退出",Command.EXIT,0);ok=newCommand("确定",Command.OK,0);display=Display.getDisplay(this);ca=newMCanvas();}protectedvoidstartApp(){form.append(tf);form.append(tf2);form.append(si);form.addCommand(ok);form.addCommand(exit);form.setCommandListener(this);display.setCurrent(form);}protectedvoidpauseApp(){}protectedvoiddestroyApp(booleanagr){}publicvoidcommandAction(Commandc,Displayabled){if(c==ok){try{showstr=split(tf2.getString(),'|');xystr=split(tf.getString(),'|');x=Integer.parseInt(xystr[0]);y=Integer.parseInt(xystr[1]);wait=Integer.parseInt(xystr[2]);display.setCurrent(ca);}catch(Exceptione){display.setCurrent(newAlert(null,"异常:n"+e.toString(),null,AlertType.INFO));}}elseif(c==exit){notifyDestroyed();}}publicString[]split(Stringstr,charsc){Integer[]index=null;String[]string=null;intin=0;intstart=0;Characterch=newCharacter(sc);for(inti=0;i<str.length();i++){if(ch.equals(str.charAt(i))){index[in]=i;string[in]=str.substring(start,index[in]);start=start+index[in]+1;in++;}}ind=in+1;returnstring;}classMCanvasextendsCanvasimplementsRunnable{inti=0;//Fontf=null,f2=null;publicMCanvas(){setFullScreenMode(true);Threadth=newThread(this);th.start();}protectedvoidpaint(Graphicsg){g.setColor(0xE96FFF);g.fillRect(0,0,getWidth(),getHeight());g.setColor(0xD2FFD9);g.fillRect(0,20,getWidth(),getHeight()-40);g.setColor(0xFF0A2E);Fontf=Font.getFont(0,1,8);//f2=Font.getFont(defaultFont);g.setFont(f);g.drawString("刷新时间:"+wait+"毫秒(ms)",20,25,0);g.drawString("起点坐标:"+"("+x+","+y+")",20,43,0);g.drawString("空闲内存:"+Runtime.getRuntime().freeMemory()/1024+"KB",20,70,0);g.drawString("退出",190,301,0);//g.setFont(f2);g.setColor(0x0000FF);//g.drawString(showstr[i],x,y,0);}publicvoidkeyPressed(intkey){if(key==-7)notifyDestroyed();}publicvoidrun(){while(true){try{Thread.sleep(50+wait);}catch(Exceptione){display.setCurrent(newAlert(null,"异常:n"+e.toString(),null,AlertType.INFO));}i++;if(i==ind)i=0;repaint();}}}}j2me没有String.split方法,我就自己试着写个split,编译后,写好坐标,刷新时间,及现实内容,(上面的是自定义,以“|”为分割符,)可是…可是,当点击 ,确定,把Display交给Canvas显示时居然Null Pointer Exception我调试下,实在看不出来哪里出了问题,所以我把源码贴上来,请高手看看吧!上课写的,手机差点被老师没收…汗、高手看看吧!谢谢了,上面太太乱的话,我给地址,这是源码地址http://agrj.cn//files/tejava_812.txt这是帖子地址http://agrj.cn/topic81436.html谢谢大家了,我是高三学生,是用手机写的,源码可编译,就是运行错误,手机调试半天,马上崩溃了!
解决方案
解决方案二:
你的代码实在没法看,也没人会看的!~重新发吧!~
解决方案三:
把代码排版好了再发出来吧。这样的代码不光我不想看,恐怕你也不想看吧。
解决方案四:
这代码,怎么看!
解决方案五:
恩,好的,谢谢大家了是这个函数的问题,大家看看吧!publicString[]split(Stringstr,charsc){String[]string=null;intin=0,start=0,index=0;Characterch=newCharacter(sc);for(inti=0;i<str.length();i++){if(ch.equals(str.charAt(i))){index=i;string[in]=str.substring(start,index);start=start+index+1;in++;}}if(in==0)string=null;returnstring;}
解决方案六:
有点乱
解决方案七:
把异常栈贴出来看看(厉害!居然用手机也能写程序!)
解决方案八:
该回复于2011-03-04 16:47:47被版主删除
解决方案九:
什么手机啊这么牛掰?
解决方案十:
没有对传入的参数进行空指针判断
解决方案十一:
String[]string=null;intin=0,start=0,index=0;Characterch=newCharacter(sc);for(inti=0;i<str.length();i++){if(ch.equals(str.charAt(i))){index=i;string[in]=str.substring(start,index);string数组没有new过吧,你的变量名字也太难看了