IllegalArgumentException al usar el componente nativo de Android en LWUIT

I am trying to use native components in a LWUIT app on Android platform.

private Component createNativeTextEdit() {
    final Object[] result = new Object[1];

    AndroidImplementation.runOnAndroidUIThreadAndWait(LWUITActivity.currentActivity, new Runnable() {
        @Override
        public void run() {
            EditText nativeView = new EditText(LWUITActivity.currentActivity);
            nativeView.setText("Type here..");

            result[0] = PeerComponent.create(nativeView);
        }
    });

    return (Component)result[0];   
}

I then place a component inside a form:

mMainForm = new Form();
mMainForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

mMainForm.addComponent(createNativeTextEdit());

mMainForm.show();

Then I get an "IllegalArgumentException: width and height must be > 0" when the system tries to draw the new form.

I traced the problem down to the call to AndroidImplementation.PeerWrapper.getBuffer() and the values returned by getWidth() and getHeight() are width=474 and height=0.

How is height supposed to be set? What am I missing?

Do you know of a working sample program that uses PeerComponent on Android? I searched the web but could only find some snippets where it is not clear where the code is called from, which thread it is executed on etc.

Gracias.

preguntado el 01 de febrero de 12 a las 22:02

2 Respuestas

Descubrí el problema.

I am using LWUIT 1.5 thorsten_s port for Android.

The issue is that LWUIT computes components preferred size before the native view is added so both width and height are set to 0.

The way to fix it is to call View.measure() on the PeerWrapper at creation time to initialize it to preferred size.

Inside AndroidImplementation.java at the end of the constructor for PeerWrapper class add the line:

measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

This completely fixes the problem. I can now use components like AnalogClock, DatePicker, and native EditText in LWUIT forms.

Respondido 04 Feb 12, 03:02

This sort of thing works for us in the Nombre clave uno implementation. I'm not exactly sure what is happening in Thorsten's port since we are pretty much forked by now. We intend to release Codename One with a native access demo that will demonstrate access to native widgets.

Respondido 02 Feb 12, 14:02

Thank you Shai! Is there a way I can get my hands on your port? Unfortunately I need a solution rather soon. Thanks. - Alex Gdalevich

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.