分享
 
 
 

【J2ME】 Debug 笔记(一)

王朝java/jsp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

娘的,买了本烂书,一个简单的Demo开始就有问题,我照着编居然有错了

代码如下:

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class DipplayTest extends MIDlet {

private Display display;

private TextBox t;

public DipplayTest() {

//super();

display = Display.getDisplay(this);

t = new TextBox("DisPlay Text","Display Object Test",0,0);

}

public void startApp() {

display.setCurrent(t);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

}

编译错误,告诉我是参数问题,排查了半天,发现TextBox这个构造函数去掉后就能编译成功,显然,这个函数的参数有问题了瓦

java.lang.IllegalArgumentException

at com.sun.midp.lcdui.DynamicCharacterArray.<init>(+15)

at com.sun.midp.lcdui.DynamicCharacterArray.<init>(+6)

at javax.microedition.lcdui.TextField.<init>(+53)

at javax.microedition.lcdui.TextBox.<init>(+74)

at DipplayTest.<init>(+26)

at java.lang.Class.runCustomCode(+0)

at com.sun.midp.midlet.MIDletState.createMIDlet(+19)

at com.sun.midp.midlet.Selector.run(+22)

查看TextBox文档说明

首先它的定义: public TextBox(String title,String text,int maxSize,int constraints)

Parameters:

title - the title text to be shown with the display

text - the initial contents of the text editing area, null may be used to indicate no initial content

maxSize - the maximum capacity in characters. The implementation may limit boundary maximum capacity and the actually assigned capacity may me smaller than requested. A defensive application will test the actually given capacity with getMaxSize().

constraints - see input constraints

还有一段描述的

Creates a new TextBox object with the given title string, initial contents, maximum size in characters, and constraints. If the text parameter is null, the TextBox is created empty. The maxSize parameter must be greater than zero. An IllegalArgumentException is thrown if the length of the initial contents string exceeds maxSize. However, the implementation may assign a maximum size smaller than the application had requested. If this occurs, and if the length of the contents exceeds the newly assigned maximum size, the contents are truncated from the end in order to fit, and no exception is thrown.

靠,搞了半天,我输入的maxSize有问题,传了个0进去,肯定有问题了.因为,就算textBox是空,maxSize也要大于0

改了下那个参数,果然好了,天煞的,书上有错误真好,让你Debug,让你学到更多,靠~~~~

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class DipplayTest extends MIDlet {

private Display display;

private TextBox t;

public DipplayTest() {

//super();

display = Display.getDisplay(this);

t = new TextBox("DisPlay Text","Display Object Test",20,0);

}

public void startApp() {

display.setCurrent(t);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

}

最后再给点TextBox的例子

http://www.eli.sdsu.edu/courses/fall04/cs683/notes/midletui/midletui.html#a_Toc496260825

Hi-Level UI Components

CS 683 Fall 04

Doc 19, MIDlet UI Slide # 8

TextBox

Textbox Documemtation

http://www.eli.sdsu.edu/courses/fall04/cs683/j2me/docs/api/midp/javax/microedition/lcdui/TextBox.html

Input Constraints

http://www.eli.sdsu.edu/courses/fall04/cs683/j2me/docs/api/midp/javax/microedition/lcdui/TextField.html

CS 683 Fall 04

Doc 19, MIDlet UI Slide # 9

Basic TextBox

Based on example, pages 89-90, J2ME in a Nutshell, Kim Topley, O’Reilly

import java.io.*;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.MIDlet;

public class TextBoxMIDlet extends MIDlet {

private static final int MAX_TEXT_SIZE = 80;

protected TextBox textBox;

protected Display display;

public TextBoxMIDlet() {

textBox = new TextBox("TextBox Example", readFile("/Text.txt"),

MAX_TEXT_SIZE, TextField.ANY);

Ticker ticker = new Ticker("This is a ticker...");

textBox.setTicker(ticker);

display = Display.getDisplay(this);

display.setCurrent(textBox);

}

public void startApp() { }

public void pauseApp() { }

public void destroyApp( boolean unconditional ) { }

CS 683 Fall 04

Doc 19, MIDlet UI Slide # 10

Basic TextBox Continued private String readFile(String filename) {

try {

InputStream byteInput = getClass().getResourceAsStream(filename);

InputStreamReader characterInput = new InputStreamReader(byteInput);

char[] buffer = new char[32];

StringBuffer stringBuffer = new StringBuffer();

int count;

while ((count = characterInput.read(buffer, 0, buffer.length)) > -1) {

stringBuffer.append(buffer, 0, count);

}

return stringBuffer.toString();

}

catch (Exception ioProblem) {

return "Could not read file";

}

}

}

CS 683 Fall 04

Doc 19, MIDlet UI Slide # 11

TextBox with Timer

Based on examples, pages 73-74, 89-90, J2ME in a Nutshell

import java.io.*;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.MIDlet;

import java.util.*;

public class ThreadTextBoxMIDlet extends MIDlet {

private static final int MAX_TEXT_SIZE = 80;

protected TextBox textBox;

private Timer timer;

private int count = 0;

public ThreadTextBoxMIDlet() {

textBox = new TextBox("Timer Example", "Hello for now",

MAX_TEXT_SIZE, TextField.ANY);

Display.getDisplay(this).setCurrent(textBox);

}

public void startApp() {

if (timer == null)

startTimer();

else

count = 0;

}

CS 683 Fall 04

Doc 19, MIDlet UI Slide # 12

TextBox with Timer Continued

public void pauseApp() {

textBox.setString("Here we go again");

}

public void destroyApp( boolean unconditional ) {

stopTimer();

}

private void startTimer() {

TimerTask addCount = new TimerTask() {

public void run() {

textBox.setString( "" + count++);

if (count == 10)

notifyPaused();

else if (count > 15)

resumeRequest();

}

};

timer = new Timer();

timer.schedule(addCount, 2000, 1000);

}

private void stopTimer() {

if (timer != null)

timer.cancel();

}

}

CS 683 Fall 04

Doc 19, MIDlet UI Slide # 13

TextBox with Command

Based on example, pages 99-100, J2ME in a Nutshell

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Displayable;

public class CommandTextBoxMIDlet extends TextBoxMIDlet

implements CommandListener {

private static final Command EXIT_COMMAND =

new Command("Exit", Command.EXIT, 0);

private static final Command OK_COMMAND =

new Command("OK", Command.OK, 0);

private static final Command CLEAR_COMMAND =

new Command("Clear", Command.SCREEN, 1);

private static final Command REVERSE_COMMAND =

new Command("Reverse", Command.SCREEN, 1);

public CommandTextBoxMIDlet() {

super();

textBox.addCommand(OK_COMMAND);

textBox.addCommand(EXIT_COMMAND);

textBox.addCommand(CLEAR_COMMAND);

textBox.addCommand(REVERSE_COMMAND);

textBox.setCommandListener(this);

}

CS 683 Fall 04

Doc 19, MIDlet UI Slide # 14

TextBox with Command Continued public void commandAction(Command c, Displayable d) {

if (c == EXIT_COMMAND) {

destroyApp(true);

notifyDestroyed();

} else if (c == OK_COMMAND) {

textBox.setString("OK pressed");

} else if (c == CLEAR_COMMAND) {

textBox.setString(null);

} else if (c == REVERSE_COMMAND) {

String str = textBox.getString();

if (str != null) {

StringBuffer sb = new StringBuffer(str);

textBox.setString(sb.reverse().toString());

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有