Repository navigation
Failed resolving method setChecked on class android.widget.Switch #943
Copy link
Copy link
Closed
Description
From @LunicLynx on August 14, 2017 22:49
The Problem
When using an ngIf on a StackLayout including a Switch like so:
<StackLayout *ngIf="data | async; let data">
<Switch [(ngModel)]="data.toggle"></Switch>
</StackLayout>Using a two-way binding. One will get the following error:
JS: ERROR Error: java.lang.Exception: Failed resolving method setChecked on class android.widget.Switch
JS: com.tns.Runtime.resolveMethodOverload(Runtime.java:1004)
JS: com.tns.Runtime.callJSMethodNative(Native Method)
JS: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1043)
JS: com.tns.Runtime.callJSMethodImpl(Runtime.java:925)
JS: com.tns.Runtime.callJSMethod(Runtime.java:912)
JS: com.tns.Runtime.callJSMethod(Runtime.java:896)
JS: com.tns.Runtime.callJSMethod(Runtime.java:888)
JS: com.tns.gen.java.lang.Runnable.run(Runnable.java:10)
JS: android.os.Handler.handleCallback(Handler.java:733)
JS: android.os.Handler.dispatchMessage(Handler.java:95)
JS: android.os.Looper.loop(Looper.java:136)
JS: android.app.ActivityThread.main(ActivityThread.java:5001)
JS: java.lang.reflect.Method.invokeNative(Native Method)
JS: java.lang.reflect.Method.invoke(Method.java:515)
JS: com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
JS: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
JS: dalvik.system.NativeStart.main(Native Method)
This seems to be a general problem with ngModel and Switch, whenever the value gets lazily initialized this error seems to happen.
Platforms
Android
Versions
- CLI: 3.1.3
package.json
{
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"readme": "NativeScript Application",
"repository": "<fill-your-repository-here>",
"nativescript": {
"id": "org.nativescript.ngIfSwitchIssue",
"tns-android": {
"version": "3.1.1"
}
},
"dependencies": {
"@angular/animations": "~4.2.0",
"@angular/common": "~4.2.0",
"@angular/compiler": "~4.2.0",
"@angular/core": "~4.2.0",
"@angular/forms": "~4.2.0",
"@angular/http": "~4.2.0",
"@angular/platform-browser": "~4.2.0",
"@angular/router": "~4.2.0",
"nativescript-angular": "~4.2.0",
"nativescript-theme-core": "~1.0.2",
"reflect-metadata": "~0.1.8",
"rxjs": "~5.4.2",
"tns-core-modules": "~3.1.0",
"zone.js": "~0.8.2"
},
"devDependencies": {
"babel-traverse": "6.4.5",
"babel-types": "6.4.5",
"babylon": "6.4.5",
"lazy": "1.0.11",
"nativescript-dev-typescript": "~0.5.0",
"typescript": "~2.4.2"
}
}Repo that reproduces the error
I created a repo reproducing the error. Just start it and wait for 5 Seconds.
https://github.com/LunicLynx/ns-ngif-switch-ngmodel-async-issue
Copied from original issue: NativeScript/NativeScript#4694
Reactions are currently unavailable
Activity
NickIliev commentedon Aug 15, 2017
From @LunicLynx on August 14, 2017 22:52
This is a workaround:
But i think it should work either way.
NickIliev commentedon Aug 15, 2017
From @EddyVerbruggen on August 15, 2017 8:43
I'm not sure if it helps for your case, but have you tried this?
sis0k0 commentedon Aug 16, 2017
related to #846
LunicLynx commentedon Aug 18, 2017
@NickIliev it's not possilbe to use the
?.operator in two way bindings.jarrodchesney commentedon Aug 25, 2017
This worked for me, I've made the names more generic, but otherwise this is my code.
My XML/HTML
My TypeScript
flynsequeira commentedon Feb 7, 2019
Facing this issue with Text Field:
<TextField hint="Eg. Ford" [(ngModel)]="car.make" class="car-list-even" required>
and on ngOnInit() I have this.car['make']='Ford' and it crashes with the above error.
justintoth commentedon Dec 13, 2021
I'm hitting this issue with the Switch widget, this was NOT resolved...
arLevi commentedon Apr 14, 2023
Wanted to share my workaround until this is resolved, in my case, I had a list of items which next to each I wanted to give a
Switchto turn on/off .. it was a part of a list ( for loop ) so I had to have some kind of data-binding for each switch on the items list.I wrote it like so:
And to answer the above question what was missing for you was also the
$eventso an example of how I used it as a list vs a single switch - I passed bothindexand$eventto the function: