Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-17889] TiConvert toBoolean doesnt handle type Interger

GitHub Issuen/a
TypeImprovement
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2018-05-08T16:19:16.000+0000
Affected Version/sn/a
Fix Version/sRelease 7.3.0
ComponentsAndroid
LabelsTiconvert, android, toBoolean
ReporterCampbell Anderson
AssigneeGary Mathews
Created2014-10-23T03:58:42.000+0000
Updated2018-07-05T16:20:25.000+0000

Description

Problem

Have tracked down a pesky problem where setting the visible property of a TiLabel >> TiView to 0 (type integer) crashed the application. (see log below) Pulled SDK and updated the method to have a case where it handles an input of type Integer. Built the SDK and ran the App. All fixed. Example below of what I added. I don't know Java but it gives you an idea of what to look at.

Logs

[ERROR] :  AndroidRuntime: FATAL EXCEPTION: main
[ERROR] :  AndroidRuntime: java.lang.IllegalArgumentException: Unable to convert java.lang.Integer to boolean.
[ERROR] :  AndroidRuntime: 	at org.appcelerator.titanium.util.TiConvert.toBoolean(TiConvert.java:357)
[ERROR] :  AndroidRuntime: 	at org.appcelerator.titanium.view.TiUIView.propertyChanged(TiUIView.java:723)
[ERROR] :  AndroidRuntime: 	at ti.modules.titanium.ui.widget.TiUILabel.propertyChanged(TiUILabel.java:280)
[ERROR] :  AndroidRuntime: 	at org.appcelerator.kroll.KrollPropertyChange.fireEvent(KrollPropertyChange.java:21)
[ERROR] :  AndroidRuntime: 	at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1059)
[ERROR] :  AndroidRuntime: 	at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:347)
[ERROR] :  AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:95)
[ERROR] :  AndroidRuntime: 	at android.os.Looper.loop(Looper.java:137)
[ERROR] :  AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:4745)
[ERROR] :  AndroidRuntime: 	at java.lang.reflect.Method.invokeNative(Native Method)
[ERROR] :  AndroidRuntime: 	at java.lang.reflect.Method.invoke(Method.java:511)
[ERROR] :  AndroidRuntime: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
[ERROR] :  AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
[ERROR] :  AndroidRuntime: 	at dalvik.system.NativeStart.main(Native Method)

Changes

org.appcelerator.titanium.util.TiConvert.toBoolean(TiConvert.java:357)
public static boolean toBoolean(Object value)
	{
		if (value instanceof Boolean) {
			return (Boolean) value;
		} else if (value instanceof String) {
			return Boolean.parseBoolean(((String) value));
		} 
		// -- added handling for Integer
		else if (value instanceof Integer) {
			int numeric = (Integer) value;
			return numeric > 0;
		} 
		// /-- added handling for Integer
		else {
			throw new IllegalArgumentException("Unable to convert " + (value == null ? "null" : value.getClass().getName()) + " to boolean.");
		}
	}

Comments

  1. Petr Cervenka 2017-11-01

    Having same issues..
  2. Rodrigo Farfán 2018-03-04

    Hi there. This issue is very important to be solved as in JS boolean expressions can be generated with other types than boolean and strings. In many cases, using JS, we set conditional statements based on integers and even if an object is null or not. Is absolutely unpaired, I mean, that it should be fixed so that we can use JS in all its extents.
  3. Gary Mathews 2018-03-05

    master: https://github.com/appcelerator/titanium_mobile/pull/9907
  4. Lokesh Choudhary 2018-05-07

    FR Passed. Waiting for merge to be enabled.
  5. Lokesh Choudhary 2018-05-08

    PR merged.
  6. Rodrigo Farfán 2018-05-25

    Sorry, but I still miss a
       else if (value == null) {
          return false;
       }
       
    before the IllegalArgumentException throwing. We need to pair "Java World" as much as possible to "JavaScript World".
  7. Lokesh Choudhary 2018-07-05

    Verified the fix in SDK 7.3.0.v20180703051211. Closing. Studio Ver: 5.1.0.201806291005 SDK Ver: 7.3.0.v20180703051211 OS Ver: 4.2.13 Xcode Ver: 7.0.4 Appc NPM: 1.1.3 Appc CLI: 10.13.5 Daemon Ver: Xcode 9.4.1 Ti CLI Ver: 5.1.1 Alloy Ver: 1.12.0 Node Ver: 8.9.1 NPM Ver: 5.5.1 Java Ver: 10.0.1 Devices: ⇨ google Nexus 5 --- Android 6.0.1 ⇨ google Nexus 6P --- Android 8.1.0 Emulator: ⇨ Android 4.1.2

JSON Source