Skip to main content

Android Native Component Exception

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
4 replies [Last post]
gt8887b
Offline
Joined: 2010-03-15

Hello,

I am running into a problem trying to use native control in an Android application.

I developed a very simple test class that wraps a native TextView

<div>public class PtNumericField extends Container {</div><div> </div><div>    private TextView mNativeView;</div><div>    </div><div>    /**</div><div>     * </div><div>     */</div><div>    public PtNumericField() {</div><div>        setLayout(new BorderLayout());</div><div>        </div><div>        AndroidImplementation.runOnAndroidUIThreadAndWait(LWUITActivity.currentActivity, new Runnable() {</div><div>            public void run()</div><div>            {</div><div>                init();</div><div>            }</div><div>        });</div><div>        </div><div>        PeerComponent p = PeerComponent.create(mNativeView);</div><div>        </div><div>        this.addComponent(BorderLayout.CENTER, p);</div><div>    }</div><div>    </div><div>    private void init() {</div><div>        Activity activity = LWUITActivity.currentActivity;</div><div>        mNativeView = new TextView(activity);</div><div>        </div><div>        mNativeView.setRawInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);</div><div>        mNativeView.setMinimumHeight(10);</div><div>        mNativeView.setMinimumWidth(20);</div><div>    }</div><div>}</div>

However, when I try to use an instance of PtNumericField inside a Form I get the following exception

<div>01-31 00:07:26.704: E/AndroidRuntime(11778): java.lang.IllegalArgumentException: width and height must be &gt; 0</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.graphics.Bitmap.nativeCreate(Native Method)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.graphics.Bitmap.createBitmap(Bitmap.java:695)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at com.sun.lwuit.impl.android.AndroidImplementation$PeerWrapper.getBuffer(AndroidImplementation.java:1767)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at com.sun.lwuit.impl.android.AndroidImplementation$PeerWrapper.drawChild(AndroidImplementation.java:1742)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewGroup.drawChild(ViewGroup.java:1730)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewGroup.drawChild(ViewGroup.java:1730)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.View.draw(View.java:6988)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.widget.FrameLayout.draw(FrameLayout.java:357)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewGroup.drawChild(ViewGroup.java:1732)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.View.draw(View.java:6988)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.widget.FrameLayout.draw(FrameLayout.java:357)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1921)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewRoot.draw(ViewRoot.java:1632)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewRoot.performTraversals(ViewRoot.java:1335)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.os.Handler.dispatchMessage(Handler.java:99)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.os.Looper.loop(Looper.java:150)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at android.app.ActivityThread.main(ActivityThread.java:4379)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at java.lang.reflect.Method.invokeNative(Native Method)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at java.lang.reflect.Method.invoke(Method.java:507)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:846)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)</div><div>01-31 00:07:26.704: E/AndroidRuntime(11778): <span class="Apple-tab-span"> </span>at dalvik.system.NativeStart.main(Native Method)</div>

What am I doing wrong? This error happens even when I include setMinimumHeight and setMinimumWidth calls.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
gt8887b
Offline
Joined: 2010-03-15

I created a simpler example that still does not work.

<div><span class="Apple-tab-span">	</span>private Component createNativeTextEdit() {</div><div><span class="Apple-tab-span">	</span>    final Object[] result = new Object[1];</div><div><span class="Apple-tab-span">	</span>    </div><div><span class="Apple-tab-span">	</span>    AndroidImplementation.runOnAndroidUIThreadAndWait(LWUITActivity.currentActivity, new Runnable() {</div><div>            @Override</div><div>            public void run() {</div><div>                EditText nativeView = new EditText(LWUITActivity.currentActivity);</div><div>                nativeView.setText(&quot;Type here..&quot;);</div><div>                </div><div>                result[0] = PeerComponent.create(nativeView);</div><div>            }</div><div><span class="Apple-tab-span">	</span>    });</div><div><span class="Apple-tab-span">	</span>    </div><div><span class="Apple-tab-span">	</span>    return (Component)result[0];   </div><div><span class="Apple-tab-span">	</span>}</div>

Then I add the peer component to a Form

<div>mMainForm = new Form();</div><div>mMainForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));</div><div> </div><div>mMainForm.addComponent(createNativeTextEdit());</div><div> </div><div>mMainForm.show();</div><div> </div><div> </div>

I've added a Log.d() call inside AndroidImplementation.PeerWrapper.getBuffer() call to print the values returned by getWidth() and getHeight() and it returns width=474 and height=0

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

Thanks

vprise
Offline
Joined: 2003-11-07
gt8887b
Offline
Joined: 2010-03-15

Do you know of a code sample 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.

gt8887b
Offline
Joined: 2010-03-15

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.