【问题标题】:RadListView cannot read property 'picture' of undefined (Android)RadListView 无法读取未定义的属性“图片”(Android)
【发布时间】:2019-03-06 02:06:33
【问题描述】:

我正在使用 RadListView 并且有一个使用 loadOnDemand 加载更多图像的列表。每隔一段时间,我就会在 Android 上收到一个令人讨厌的错误,上面写着 Cannot read property 'picture' of undefined,它会让整个应用程序冻结一段时间。

这很令人沮丧,因为我一直认为我会在一段时间内没有发生时修复它,但随后它会随机返回。我认为一个完整的项目重构可能会解决这个问题,但它没有。

我创建了一个有问题的示例项目。 https://github.com/keerl/radlistview-bug.

我还为它创建了一个 Playground:https://play.nativescript.org/?id=H7lJuH&template=play-ng&v=3

我还录制了有关问题发生的视频。 https://www.youtube.com/watch?v=_EGvw8REek8。这个问题是非常随机的,有时它永远不会触发(这让我觉得我已经修复了它),然后随机数小时后又开始再次发生。仅在 Android 上发生。我尝试使用插件进行图像缓存 (WebImage),认为图像加载会导致问题,也许占位符会有所帮助,但这并没有解决问题。

【问题讨论】:

    标签: nativescript angular2-nativescript nativescript-angular nativescript-telerik-ui


    【解决方案1】:

    我已经为你更新了playground。你太早通知列表notifyPullToRefreshFinished

    您应该仅在加载数据时通知。例如

    loadProducts(PullToRefreshInitiated: boolean) {
            return new Promise((resolve, reject) => {
                http.getJSON("https://randomuser.me/api/?results=25&inc=picture").then((r) => {
                    this.users$.push(r.results);
                    if (PullToRefreshInitiated) {
                        this.listView.notifyPullToRefreshFinished();
                    }
                    // Load a max of 50 items and then force a pull-to-refresh to get more data.
                    if (this.users$.length > 50) {
                        resolve(true);
                    }
                    else {
                        resolve(false);
                    }
                }, (e) => {
                    console.log(e)
                });
            });
        }
    

    附:我个人在类似环境中实施的另外两个建议。

    1. 使用图片cache
    2. 在例如使用*ngIf <Image *ngIf="item" [src]="item.picture.medium"></Image>,你可以使用一个标签作为你想要的 else 块。

    【讨论】:

    • 感谢您的回复。我运行了您更新的 Playground,但我仍然立即收到“无法读取未定义的属性‘图片’”。我尝试在图片上使用*ngIf="item",但现在我根本无法滚动,它似乎只能加载 6 张图片。
    • 当你尝试 *ngIf 时,你应该将 Image 作为空图像标签放在 stacklayout 中(如果项目未定义)会给你错误
    • 似乎没有工作。我收到“在列表模板中找不到合适的视图!”现在出错。尝试了 StackLayout 中的 Image 和 else 语句,没有运气。
    • 是的,这就是我提到的在“item”由于 *ngIf 而未定义的情况下,ng-template 将不会呈现并且您将收到 No proper view 错误。这就是为什么我建议创建一个组件并在该组件中包含该图像或使用 *ngIf 的 elsebock
    • 好的,我似乎可以正常工作。请参阅更新的playground。它在最初的应用程序启动时效果很好,但是当我在 Playground 中进行更改并重新同步到我的手机时,一切都冻结了,无法真正滚动。
    猜你喜欢
    • 2018-02-28
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多