Nativescript轮播控件不起作用

时间:2018-11-18 03:11:39

标签: nativescript nativescript-angular

我正在尝试使用Nativescript-Carousel插件显示多个Google地方信息。但是,每当我为Carousel控件设置数据集时,我都会遇到以下异常情况

System.err: java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 20 Pager id: 9 Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.tns.gen.android.support.v4.view.PagerAdapter_index_222_28_CarouselPagerAdapterClassInner
System.err:     at android.support.v4.view.ViewPager.populate(ViewPager.java:1135)
System.err:     at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
System.err:     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at org.nativescript.widgets.CommonLayoutParams.measureChild(CommonLayoutParams.java:262)
System.err:     at org.nativescript.widgets.MeasureHelper.measureChildFixedColumnsAndRows(GridLayout.java:1055)
System.err:     at org.nativescript.widgets.MeasureHelper.measure(GridLayout.java:865)
System.err:     at org.nativescript.widgets.GridLayout.onMeasure(GridLayout.java:279)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at org.nativescript.widgets.CommonLayoutParams.measureChild(CommonLayoutParams.java:262)
System.err:     at org.nativescript.widgets.MeasureHelper.measureChildFixedColumnsAndRows(GridLayout.java:1055)
System.err:     at org.nativescript.widgets.MeasureHelper.measure(GridLayout.java:865)
System.err:     at org.nativescript.widgets.GridLayout.onMeasure(GridLayout.java:279)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at org.nativescript.widgets.CommonLayoutParams.measureChild(CommonLayoutParams.java:262)
System.err:     at org.nativescript.widgets.MeasureHelper.measureChildFixedColumnsAndRows(GridLayout.java:1055)
System.err:     at org.nativescript.widgets.MeasureHelper.measure(GridLayout.java:865)
System.err:     at org.nativescript.widgets.GridLayout.onMeasure(GridLayout.java:279)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at org.nativescript.widgets.CommonLayoutParams.measureChild(CommonLayoutParams.java:262)
System.err:     at org.nativescript.widgets.ContentLayout.onMeasure(ContentLayout.java:32)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
System.err:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
System.err:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
System.err:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
System.err:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
System.err:     at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
System.err:     at android.view.View.measure(View.java:19857)
System.err:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
System.err:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
System.err:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
System.err:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
System.err:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
System.err:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
System.err:     at android.view.Choreographer.doCallbacks(Choreographer.java:686)
System.err:     at android.view.Choreographer.doFrame(Choreographer.java:621)
System.err:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
System.err:     at android.os.Handler.handleCallback(Handler.java:751)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
System.err:     at android.os.Looper.loop(Looper.java:154)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是我的代码

this.gpsLocationService.getGpsLocation()
            .then((location) => {

                this.latitude = location[0];
                this.longitude = location[1];

                this.nearesthelpService.getNearByPlaces(this.latitude, this.longitude, 5000, 'pharmacy')
                    .subscribe((res) => {
                        this.places = (<any>res).results;
                        console.dir(this.places)
                        let placeDetails: PlaceDetails[] = []
                        this.places.forEach((place) => {
                            this.nearesthelpService.getNearByPlaceDetails(place.reference).subscribe((details: PlaceDetails) => {
                                console.log(details.result.name);
                                placeDetails.push(details);

                                if (placeDetails.length === this.places.length){
                                    this.placeDetails = placeDetails;
                                }
                            })
                        });
                    });
            });

这也是我的模板

<GridLayout row="1" *ngIf="placeDetails.length > 0">
        <Carousel #carousel (pageChanged)="onPageChanged($event)">
            <ng-template ngFor let-subitem [ngForOf]="placeDetails">
                <CarouselItem>
                    <GridView rows="auto,*">
                        <Label [text]="placedetail.result?.name"></Label>
                    </GridView>
                </CarouselItem>
            </ng-template>
        </Carousel>
    </GridLayout>

我尝试了所有不同的方法,以使附加到Carousel控件的数据集不会再次更改。从上面可以看出,我还首先在本地对象中计算结果,然后将其附加到类变量。但是没有运气。知道为什么它失败了吗?

只需添加一下,如果我将数据源分配为静态数组而不是放置数组,则此控件不会引发任何问题。但是,当通过API结果分配结果时,它将无法正常工作。

只需添加一下,我在插件页面上看到,无法动态更改角度内容。但是,我的要求是一旦收到内容就不要更改内容。

1 个答案:

答案 0 :(得分:0)

很遗憾,这个问题没有答案。 Github页面提到了

(角度)仅静态幻灯片有效,带有绑定的动态幻灯片无效(目前)。

我假设第一次将数据分配给空数组仍然是静态幻灯片。但是,看起来情况并非如此。如果您提前准备好数据,则可以在初始化时对其进行分配。