手机站
网通分站
电信主站
密 码:
用户名:
热门关键字:  虚拟主机  cn域名  域名注册  非法  seo
当前位置 : 主页>程序设计>Java技术>列表

【J2ME】 Debug 笔记(一)

来源:互联网 作者:west263.com 时间:2008-02-23 点击:
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

娘的,买了本烂书,一个简单的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

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

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 ) { }

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";

      }

   }

}


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

共2页: 上一页 1 [2] 下一页
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名
注册