【发布时间】:2020-03-31 20:34:18
【问题描述】:
可能是一个愚蠢的问题,但我正在尝试获取远程数据以在 nativescript-ui-autocomplete 中使用,但我收到以下错误 ERROR TypeError: Cannot set property 'loadSuggestionsAsync' of undefined
代码与https://github.com/NativeScript/nativescript-ui-samples-angular/tree/master/autocomplete/app/examples/remote-data-fetch 的示例非常相似,但我无法让我的代码工作。
打字稿
import * as http from "tns-core-modules/http";
import { ObservableArray } from "tns-core-modules/data/observable-array";
import { TokenModel } from "nativescript-ui-autocomplete";
import { RadAutoCompleteTextViewComponent } from "nativescript-ui-autocomplete/angular";
export class MapComponent implements OnInit {
private _items: ObservableArray<TokenModel>;
private jsonUrl = "https://raw.githubusercontent.com/NativeScript/nativescript-ui-samples/master/examples-data/airports.json";
mapbox: MapboxViewApi;
constructor( private router: Router) {
// Use the component constructor to inject providers.
}
ngOnInit() {
let that = this;
this.autocomplete.autoCompleteTextView.loadSuggestionsAsync = function (text) {
const promise = new Promise(function (resolve, reject) {
http.getJSON(that.jsonUrl).then(function (r: any) {
const airportsCollection = r.airports;
const items: Array<TokenModel> = new Array();
for (let i = 0; i < airportsCollection.length; i++) {
items.push(new TokenModel(airportsCollection[i].FIELD2, null));
}
resolve(items);
}).catch((err) => {
const message = 'Error fetching remote data from ' + that.jsonUrl + ': ' + err.message;
console.log(message);
alert(message);
reject();
});
});
return promise;
};
}
@ViewChild("autocomplete", { static: true }) autocomplete: RadAutoCompleteTextViewComponent;
get dataItems(): ObservableArray<TokenModel> {
return this._items;
}
}
XML
<RadAutoCompleteTextView #autocomplete [items]="dataItems" suggestMode="Suggest" displayMode="Plain">
<SuggestionView tkAutoCompleteSuggestionView suggestionViewHeight="300">
<ng-template tkSuggestionItemTemplate let-item="item">
<StackLayout orientation="vertical" padding="10">
<Label [text]="item.text"></Label>
</StackLayout>
</ng-template>
</SuggestionView>
</RadAutoCompleteTextView>
我也在一个空白项目上尝试过,但同样的错误
编辑: 尝试在操场上重现,它没有问题,我将代码复制到我的项目并得到相同的错误,尝试将所有内容放入 try/catch 并在错误后得到这个
ERROR Error: Uncaught (in promise): Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
JS: createAlertDialog(file: node_modules\@nativescript\core\ui\dialogs\dialogs.android.js:12:0)
JS: at (file: node_modules\@nativescript\core\ui\dialogs\dialogs.android.js:96:0)
JS: at ZoneAwarePromise(file: node_modules\@nativescript\angular\zone-js\dist\zone-nativescript.js:902:0)
JS: at alert(file: node_modules\@nativescript\core\ui\dialogs\dialogs.android.js:93:0)
JS: at push../app/map/map.component.ts.MapComponent.ngAfterViewInit(file: src\app\map\map.component.ts:61:12)
JS: at callProviderLifecycles(file: node_modules\@angular\core\fesm5\core.js:21414:0)
JS: at callElementProvidersLifecycles(file: node_modules\@angular\core\fesm5\core.js:21388:0)
JS: at callLifecycleHooksChildrenFirst(file:///data/data/org.nativescript.MapboxEnduco/files/app...
【问题讨论】:
-
你能分享一个可以重现问题的 Playground 示例吗?
-
@Manoj 更新了原帖
-
查看错误是从警报对话框本身引发的。如果您评论所有对话框或可能将其替换为日志,您仍然会看到相同的错误吗?
-
@Manoj 是的,这是我写的 try/catch 的问题,不再发生。奇怪的是我将示例中的代码复制到操场上,工作正常。我下载了操场并在本地运行它,工作正常。我用 tns 创建一个新的 Angular 应用程序并复制代码,但不起作用。
标签: nativescript nativescript-angular nativescript-telerik-ui nativescript-plugin