1

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);
}
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Alan Lai
  • 1,296
  • 2
  • 12
  • 16
  • 2
    I would suggest you to review event log on device or stack trace on the simulator. Somewhere you have recursion - some relayout cause action that triggers next relayout. Please check that you don't set/clear text in paint or relayout – Eugen Martynov Jul 03 '12 at 15:39
  • please post code showing how you **use** this class. for example, the `Manager` subclass that adds instances of `Custom_EditField`. i just built a sample with this class, and although I'm not sure it works the way you want it to, it does not create any stack overflows for me. – Nate Jul 04 '12 at 01:45

1 Answers1

0

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.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • it works, thanks, can i get your msn or any contact? I need help in blackberry – Alan Lai Jul 04 '12 at 10:41
  • @AlanLai, if you have more coding questions, feel free to keep posting them here. I'm spending a lot of time on stackoverflow right now. If you need personal consulting, I can do that, but your profile says that you're a student, and I think you might find me a bit too expensive. But, I answer questions here for free, to keep my blackberry skills current while I'm not working on blackberry projects, and because when you post questions, there are other people with similar questions that benefit from seeing the answer. And of course, you get help for free :) – Nate Jul 04 '12 at 23:08