Question

I am trying to type in characters to fill an editfield, if the text is still on the first line I have got no problem but when the first line is full then it goes to the next line and I get the following error:

0:01:56.609: Uncaught: StackOverflowError

Here is my custom_editfield:

public class Custom_EditField extends EditField {

int width, row;

Custom_EditField(long style, int width, int row) {
    super(style);
    this.width = width;
    this.row = row;
}

public int getPreferredHeight() {
    return Font.getDefault().getHeight() * row;
}

public int getPreferredWidth() {
    return width;
}

protected void layout(int maxWidth, int maxHeight) {
    super.layout(maxWidth,
            Math.min(maxHeight, Font.getDefault().getHeight() * row));
    super.setExtent(maxWidth,
            Math.min(maxHeight, Font.getDefault().getHeight() * row));
}

/*public int drawText(Graphics graphics, int offset, int length, int x,
        int y, DrawTextParam drawTextParam) {
    graphics.setBackgroundColor(Color.GRAY);
    graphics.clear();
    graphics.setColor(Color.BLACK);
    int labelWidth = getFont().getAdvance(getLabel());
    graphics.drawRect(labelWidth, 0, getWidth() - labelWidth, getHeight());
    return graphics.drawText(
            this.getText().substring(offset, offset + length), x, y);
}*/

protected void paint(Graphics graphics) {
    int rectHeight = getPreferredHeight();
    int rectWidth = getPreferredWidth();
    graphics.setBackgroundColor(Color.GRAY);
    graphics.clear();
    graphics.setColor(Color.BLACK);
    graphics.drawRect(0, 0, rectWidth, rectHeight);
    super.paint(graphics);
}
}

I also noticed that the cursor is transparent. Do you think that the cursor could cause this problem?

Here is I call the editfield

extends VerticalFIeldManager

protected void sublayout(int width, int height) {

    comments = new Custom_EditField(Field.FIELD_HCENTER
            | Field.FIELD_VCENTER | Field.FOCUSABLE,
            getPreferredWidth() - 10, 3);
    add(comments);

    namelabel = new Custom_LabelField("姓名:", DrawStyle.ELLIPSIS
            | LabelField.USE_ALL_WIDTH | DrawStyle.LEFT | Field.FIELD_LEFT);
    namelabel.setFont(Font.getDefault().derive(Font.BOLD, 20));
    namelabel.setFontColor(Color.BLACK);
    add(namelabel);

    postbtn = new ButtonField("留言", DrawStyle.HCENTER | Field.FIELD_RIGHT);
    postbtn.setPadding(0, 20, 0, 20);
    postbtn.setChangeListener(this);
    add(postbtn);

    name = new Custom_EditField(Field.FIELD_HCENTER | Field.FIELD_VCENTER
            | Field.FOCUSABLE, getPreferredWidth()
            - namelabel.getPreferredWidth() - postbtn.getContentWidth() - 5
            * 4, 1);
    add(name);

    Field field = getField(0);
    layoutChild(field, getPreferredWidth() - 10,
            comments.getPreferredHeight());
    setPositionChild(field, 5, 5);

    field = getField(1);
    layoutChild(field, Font.getDefault().getAdvance(namelabel.getText()),
            namelabel.getContentHeight());
    setPositionChild(field, 5, comments.getPreferredHeight() + 5 * 2);

    field = getField(2);
    layoutChild(field, postbtn.getPreferredWidth() + postbtn.getWidth(),
            name.getPreferredHeight());
    setPositionChild(field, getPreferredWidth() - (postbtn.getWidth() + 5),
            comments.getPreferredHeight() + 5 * 2);

    field = getField(3);
    layoutChild(field, getPreferredWidth() * 2 / 3,
            name.getPreferredHeight());
    setPositionChild(field, namelabel.getWidth(),
            comments.getPreferredHeight() + 5 * 2);

    width = Math.min(width, getPreferredWidth());
    height = Math.min(height, getPreferredHeight());
    setExtent(width, height);
}
Was it helpful?

Solution

The Solution

I still can't build your code, because the Manager code posted uses the class Custom_LabelField, which you haven't shown. However, I would assume that Custom_LabelField is somewhat like a LabelField. If I assign name to be a LabelField, then I see your StackOverflowError.

Just as @EugenMartynov said, you are setting the text of a label field inside your implementation of sublayout(). This actually causes sublayout() to be called again, from within itself. This causes namelabel to be created again, and the process repeats infinitely until the stack is exhausted.

Really, the sublayout() method should only be used for layout operations: positioning and sizing. You should normally create your Field objects before that. Normally, that happens in the constructor. If you move the first half of your sublayout method to the constructor, you'll fix this problem:

public class MyManager extends VerticalFieldManager {
   public MyManager() {
      super(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);

      comments = new Custom_EditField(Field.FIELD_HCENTER 
        | Field.FIELD_VCENTER | Field.FOCUSABLE, 
        getPreferredWidth() - 10, 3); 
      add(comments); 

      namelabel = new Custom_LabelField("姓名:", DrawStyle.ELLIPSIS 
        | LabelField.USE_ALL_WIDTH | DrawStyle.LEFT | Field.FIELD_LEFT); 
      namelabel.setFont(Font.getDefault().derive(Font.BOLD, 20)); 
      namelabel.setFontColor(Color.BLACK); 
      add(namelabel); 

      postbtn = new ButtonField("留言", DrawStyle.HCENTER | Field.FIELD_RIGHT); 
      postbtn.setPadding(0, 20, 0, 20); 
      postbtn.setChangeListener(this); 
      add(postbtn); 

      name = new Custom_EditField(Field.FIELD_HCENTER | Field.FIELD_VCENTER 
        | Field.FOCUSABLE, getPreferredWidth() 
        - namelabel.getPreferredWidth() - postbtn.getContentWidth() - 5 
        * 4, 1); 
      add(name); 
   }
}

Then sublayout() becomes simpler:

protected void sublayout(int height, int width) {
   Field field = getField(0);    
   layoutChild(field, getPreferredWidth() - 10,    
        comments.getPreferredHeight());    
   setPositionChild(field, 5, 5);    

   field = getField(1);    
   layoutChild(field, Font.getDefault().getAdvance(namelabel.getText()),    
        namelabel.getContentHeight());    
   setPositionChild(field, 5, comments.getPreferredHeight() + 5 * 2);    

   field = getField(2);    
   layoutChild(field, postbtn.getPreferredWidth() + postbtn.getWidth(),    
        name.getPreferredHeight());    
   setPositionChild(field, getPreferredWidth() - (postbtn.getWidth() + 5),    
        comments.getPreferredHeight() + 5 * 2);    

   field = getField(3);    
   layoutChild(field, getPreferredWidth() * 2 / 3,    
        name.getPreferredHeight());    
   setPositionChild(field, namelabel.getWidth(),    
        comments.getPreferredHeight() + 5 * 2);    

   width = Math.min(width, getPreferredWidth());    
   height = Math.min(height, getPreferredHeight());    
   setExtent(width, height);    
}

General Information

If you want to know how to catch a StackOverflowError, put this code in your top level UiApplication subclass (just for debugging ... remove it before releasing your app):

  try {
     MyApp theApp = new MyApp();       
     theApp.enterEventDispatcher();
  } catch (Throwable e) {
     e.printStackTrace();
  }

After it hits the line e.printStackTrace(), your Eclipse Console window will show something like this:

[375.664]  0x16 
[375.664] HelloBlackBerry(4FF4123E) 
[375.664]  MyManager 
[375.664]  sublayout 
[375.664]  0x16 
[375.664] HelloBlackBerry(4FF4123E) 
[375.664]  MyManager 
[375.664]  sublayout 
[375.664]  0x16 
[375.664] HelloBlackBerry(4FF4123E) 
[375.664]  MyManager 
[375.664]  sublayout 
[375.664]  0x16 
[375.664] HelloBlackBerry(4FF4123E) 
[375.664]  MyManager 
[375.664]  sublayout 
[375.664]  0x16 
[375.664] HelloBlackBerry(4FF4123E)

You can see, over and over, with almost the exact same timestamp (375.664), the sublayout method in the MyManager class is being called. When you see that, you know you need to look at that method, because it's calling itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top