Failed resolving method setChecked on class android.widget.Switch


#1

Hello guys, I’m trying to run the code I’ve written for chapter three of the book “NativeScript for Angular Mobile Development”, but I’m getting a pretty nasty error that I can’t seem to be rid of:


An uncaught Exception occurred on "main" thread.
com.tns.NativeScriptException: 
Calling js method getView failed

Error: java.lang.Exception: Failed resolving method setChecked on class android.widget.Switch
    com.tns.Runtime.resolveMethodOverload(Runtime.java:1049)
    com.tns.Runtime.callJSMethodNative(Native Method)
    com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
    com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
    com.tns.Runtime.callJSMethod(Runtime.java:957)
    com.tns.Runtime.callJSMethod(Runtime.java:941)
    com.tns.Runtime.callJSMethod(Runtime.java:933)
    com.tns.gen.android.widget.BaseAdapter_frnal_ts_helpers_l58_c38_ListViewAdapter.getView(BaseAdapter_frnal_ts_helpers_l58_c38_ListViewAdapter.java:47)
    android.widget.AbsListView.obtainView(AbsListView.java:2427)
    android.widget.ListView.makeAndAddView(ListView.java:1970)
    android.widget.ListView.fillDown(ListView.java:704)
    android.widget.ListView.fillFromTop(ListView.java:765)
    android.widget.ListView.layoutChildren(ListView.java:1744)
    android.widget.AbsListView.onLayout(AbsListView.java:2217)
    android.view.View.layout(View.java:17661)
    android.view.ViewGroup.layout(ViewGroup.java:5575)
    org.nativescript.widgets.CommonLayoutParams.layoutChild(CommonLayoutParams.java:222)
    org.nativescript.widgets.GridLayout.onLayout(GridLayout.java:353)
    android.view.View.layout(View.java:17661)
    android.view.ViewGroup.layout(ViewGroup.java:5575)
    org.nativescript.widgets.CommonLayoutParams.layoutChild(CommonLayoutParams.java:222)
    org.nativescript.widgets.GridLayout.onLayout(GridLayout.java:353)
    android.view.View.layout(View.java:17661)
    android.view.ViewGroup.layout(ViewGroup.java:5575)
    org.nativescript.widgets.CommonLayoutParams.layoutChild(CommonLayoutParams.java:222)
    org.nativescript.widgets.ContentLayout.onLayout(ContentLayout.java:73)
    android.view.View.layout(View.java:17661)
    android.view.ViewGroup.layout(ViewGroup.java:5575)
    android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    android.view.View.layout(View.java:17661)
    android.view.ViewGroup.layout(ViewGroup.java:5575)
    android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    android.view.View.layout(View.java:17661)
    android.view.ViewGroup.layout(ViewGroup.java:5575)
    android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    com.android.internal.policy.DecorView.onLayout(DecorView.java:731)
    android.view.View.layout(View.java:17661)
    android.view.ViewGroup.layout(ViewGroup.java:5575)
    android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
    android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
    android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
    android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6344)
    android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
    android.view.Choreographer.doCallbacks(Choreographer.java:686)
    android.view.Choreographer.doFrame(Choreographer.java:621)
    android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
    android.os.Handler.handleCallback(Handler.java:751)
    android.os.Handler.dispatchMessage(Handler.java:95)
    android.os.Looper.loop(Looper.java:154)
    android.app.ActivityThread.main(ActivityThread.java:6236)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
File: "file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/switch/switch.js, line: 57, column: 33

StackTrace: 
	Frame: function:'Switch.(anonymous function)', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/switch/switch.js', line: 57, column: 34
	Frame: function:'applyAllNativeSetters', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/properties/properties.js', line: 955, column: 28
	Frame: function:'initNativeView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/properties/properties.js', line: 899, column: 9
	Frame: function:'ViewBase.onResumeNativeUpdates', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 539, column: 22
	Frame: function:'ViewBase._resumeNativeUpdates', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 207, column: 18
	Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 184, column: 14
	Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 62, column: 35
	Frame: function:'', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 190, column: 19
	Frame: function:'LayoutBaseCommon.eachChildView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/layouts/layout-base-common.js', line: 125, column: 26
	Frame: function:'ViewCommon.eachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view-common.js', line: 689, column: 14
	Frame: function:'ViewBase._loadEachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 189, column: 14
	Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 185, column: 14
	Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 62, column: 35
	Frame: function:'ViewBase._addViewCore', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 344, column: 18
	Frame: function:'ViewBase._addView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 330, column: 14
	Frame: function:'ListViewAdapter.getView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/list-view/list-view.js', line: 252, column: 36


	at com.tns.Runtime.callJSMethodNative(Native Method)
	at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
	at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
	at com.tns.Runtime.callJSMethod(Runtime.java:957)
	at com.tns.Runtime.callJSMethod(Runtime.java:941)
	at com.tns.Runtime.callJSMethod(Runtime.java:933)
	at com.tns.gen.android.widget.BaseAdapter_frnal_ts_helpers_l58_c38_ListViewAdapter.getView(BaseAdapter_frnal_ts_helpers_l58_c38_ListViewAdapter.java:47)
	at android.widget.AbsListView.obtainView(AbsListView.java:2427)
	at android.widget.ListView.makeAndAddView(ListView.java:1970)
	at android.widget.ListView.fillDown(ListView.java:704)
	at android.widget.ListView.fillFromTop(ListView.java:765)
	at android.widget.ListView.layoutChildren(ListView.java:1744)
	at android.widget.AbsListView.onLayout(AbsListView.java:2217)
	at android.view.View.layout(View.java:17661)
	at android.view.ViewGroup.layout(ViewGroup.java:5575)
	at org.nativescript.widgets.CommonLayoutParams.layoutChild(CommonLayoutParams.java:222)
	at org.nativescript.widgets.GridLayout.onLayout(GridLayout.java:353)
	at android.view.View.layout(View.java:17661)
	at android.view.ViewGroup.layout(ViewGroup.java:5575)
	at org.nativescript.widgets.CommonLayoutParams.layoutChild(CommonLayoutParams.java:222)
	at org.nativescript.widgets.GridLayout.onLayout(GridLayout.java:353)
	at android.view.View.layout(View.java:17661)
	at android.view.ViewGroup.layout(ViewGroup.java:5575)
	at org.nativescript.widgets.CommonLayoutParams.layoutChild(CommonLayoutParams.java:222)
	at org.nativescript.widgets.ContentLayout.onLayout(ContentLayout.java:73)
	at android.view.View.layout(View.java:17661)
	at android.view.ViewGroup.layout(ViewGroup.java:5575)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
	at android.view.View.layout(View.java:17661)
	at android.view.ViewGroup.layout(ViewGroup.java:5575)
	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
	at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
	at android.view.View.layout(View.java:17661)
	at android.view.ViewGroup.layout(ViewGroup.java:5575)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
	at com.android.internal.policy.DecorView.onLayout(DecorView.java:731)
	at android.view.View.layout(View.java:17661)
	at android.view.ViewGroup.layout(ViewGroup.java:5575)
	at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6344)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
	at android.view.Choreographer.doCallbacks(Choreographer.java:686)
	at android.view.Choreographer.doFrame(Choreographer.java:621)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
	at android.os.Handler.handleCallback(Handler.java:751)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:154)
	at android.app.ActivityThread.main(ActivityThread.java:6236)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.lang.Exception: Failed resolving method setChecked on class android.widget.Switch
	at com.tns.Runtime.resolveMethodOverload(Runtime.java:1049)
	... 56 more

I’m not sure that it matters because of the nature of the error (no reference in the error to any code of mine), but here is the place where I try to use the Switch widget

<ListView [items]="playerService.tracks | orderBy:'order'">
    <ng-template let-track="item">
        <GridLayout rows="auto" columns="75, *, 100">
            <Button text="Record" (tap)="record(track)" row="0" col="0"></Button>
            <Label [text]="track.name" row="0" col="1"></Label>
            <Switch [checked]="track.solo" row="0" col="2"></Switch>
        </GridLayout>
    </ng-template>

</ListView>

and playerService.tracks is defined here:

@Injectable()
export class PlayerService{
    public playing:boolean;
    public tracks:ITrack[];
    
    constructor(){
        this.tracks = [
            {name: "Guitar"},
            {name: "Vocals"},
        ];
    }
...

so it’s not an empty list or anything.

My package,json file is as follows:

{
	"description": "NativeScript Application",
	"license": "SEE LICENSE IN <your-license-filename>",
	"readme": "NativeScript Application",
	"repository": "<fill-your-repository-here>",
	"nativescript": {
		"id": "org.nativescript.TNSStudio",
		"tns-android": {
			"version": "3.3.1"
		},
		"tns-ios": {
			"version": "3.3.0"
		}
	},
	"dependencies": {
		"@angular/animations": "~4.4.1",
		"@angular/common": "~4.4.1",
		"@angular/compiler": "~4.4.1",
		"@angular/core": "~4.4.1",
		"@angular/forms": "~4.4.1",
		"@angular/http": "~4.4.1",
		"@angular/platform-browser": "~4.4.1",
		"@angular/router": "~4.4.1",
		"nativescript-angular": "^4.4.1",
		"nativescript-theme-core": "^1.0.4",
		"reflect-metadata": "~0.1.8",
		"rxjs": "~5.4.2",
		"tns-core-modules": "^3.3.0",
		"zone.js": "~0.8.2"
	},
	"devDependencies": {
		"babel-traverse": "6.26.0",
		"babel-types": "6.26.0",
		"babylon": "6.18.0",
		"lazy": "1.0.11",
		"nativescript-dev-typescript": "~0.5.0",
		"typescript": "~2.4.2"
	}
}


#2

setChecked is not a valid method of a Switch widget
https://docs.nativescript.org/api-reference/modules/ui_switch


#3

Well yes of course, but I never tried to call setChecked myself, I only attempted to utilize the text attribute as described here.


#4

Changing

<Switch [checked]="track.solo" row="0" col="2"></Switch>

to

<Switch checked="true" row="0" col="2"></Switch>

now shows

System.err: com.tns.NativeScriptException:
System.err: Calling js method onStart failed
System.err:
System.err: TypeError: Cannot read property 'getLayoutParams' of undefined
System.err: File: "file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/layouts/grid-layout/grid-layout.js, line: 10, column: 27
System.err:
System.err: StackTrace:
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/layouts/grid-layout/grid-layout.js', line: 10, column: 28
System.err:     Frame: function:'applyAllNativeSetters', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/properties/properties.js', line: 955, column: 28
System.err:     Frame: function:'initNativeView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/properties/properties.js', line: 899, column: 9
System.err:     Frame: function:'ViewBase.onResumeNativeUpdates', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 539, column: 22
System.err:     Frame: function:'ViewBase._resumeNativeUpdates', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 207, column: 18
System.err:     Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 184, column: 14
System.err:     Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 62, column: 35
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 190, column: 19
System.err:     Frame: function:'LayoutBaseCommon.eachChildView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/layouts/layout-base-common.js', line: 125, column: 26
System.err:     Frame: function:'ViewCommon.eachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view-common.js', line: 689, column: 14
System.err:     Frame: function:'ViewBase._loadEachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 189, column: 14
System.err:     Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 185, column: 14
System.err:     Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 62, column: 35
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 190, column: 19
System.err:     Frame: function:'ContentView.eachChildView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/content-view/content-view.js', line: 70, column: 13
System.err:     Frame: function:'PageBase.eachChildView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/page/page-common.js', line: 206, column: 40
System.err:     Frame: function:'ViewCommon.eachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view-common.js', line: 689, column: 14
System.err:     Frame: function:'ViewBase._loadEachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 189, column: 14
System.err:     Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 185, column: 14
System.err:     Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 62, column: 35
System.err:     Frame: function:'Page.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/page/page.js', line: 105, column: 35
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 190, column: 19
System.err:     Frame: function:'FrameBase.eachChildView', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/frame/frame-common.js', line: 385, column: 13
System.err:     Frame: function:'ViewCommon.eachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view-common.js', line: 689, column: 14
System.err:     Frame: function:'ViewBase._loadEachChild', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 189, column: 14
System.err:     Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 185, column: 14
System.err:     Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 62, column: 35
System.err:     Frame: function:'ActivityCallbacksImplementation.onStart', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 640, column: 22
System.err:     Frame: function:'NativeScriptActivity.onStart', file:'file:///data/data/org.nativescript.TNSStudio/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 26, column: 25
System.err:
System.err:     at com.tns.Runtime.callJSMethodNative(Native Method)
System.err:     at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
System.err:     at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:957)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:941)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:933)
System.err:     at com.tns.NativeScriptActivity.onStart(NativeScriptActivity.java:38)
System.err:     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
System.err:     at android.app.Activity.performStart(Activity.java:6701)
System.err:     at android.app.Activity.performRestart(Activity.java:6775)
System.err:     at android.app.ActivityThread.handleSleeping(ActivityThread.java:4086)
System.err:     at android.app.ActivityThread.-wrap23(ActivityThread.java)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
System.err:     at android.os.Looper.loop(Looper.java:154)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6236)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
ActivityManager: Process org.nativescript.TNSStudio (pid 30525) has died

#5

I resolved the problem by generating a new project using tns create blank --ng and migrating the contents of the old app folder to the app folder in the new project.