Frage

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.

Thanks.

War es hilfreich?

Lösung

I figured out the issue.

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.

Andere Tipps

This sort of thing works for us in the Codename One 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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top