Skip to content

TabView crashes upon tab navigation #857

Open
@tsonevn

Description

@tsonevn

From @nilsmehlhorn on June 14, 2017 11:52

Hey, I've got the exact same issue as described in #4317 after updating my project to NS 3.0, however adding a simple ngIf did not do the trick for me. As I have not changed any of my code since the update I'm fairly certain that it is a bug in the TabView. I'll fill out the form just like @AlvSovereign did, you'll see its almost the same:

Which platform(s) does your issue occur on?

Android

Please provide the following version numbers that your issue occurs with:

CLI: 3.0.3
Cross-platform modules: 3.01
Runtime(s): Node: 7.8.0, npm: 4.2.0
Plugin(s):

  • "nativescript-angular": "^3.0.0",
  • "nativescript-cardview": "^2.0.1",
  • "nativescript-floatingactionbutton": "^3.0.0",
  • "nativescript-imagepicker": "3.0.1" (built locally)
  • "nativescript-permissions": "^1.2.3",
  • "nativescript-plugin-firebase": "^3.11.3",
  • "nativescript-theme-core": "~1.0.4",

Please tell us how to recreate the issue in as much detail as possible.

Scroll across to the 3rd tab in the tab view and the app crashes. These tabs are dynamically added via Angular's *ngFor directive on the tag.

Interestingly enough, there is no data being shown on the first and second tab. When it gets to the third, there is an image from the API call it makes, but as it comes to view, it crashes.

It is the same for me. The first two tabs are showing no data initially though they should (only if add something, as my tabs are presenting dynamic lists in them). When I get to the third tab, I see its contents being displayed correctly for a second, then the application crashes with the following error message:

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

Error: Expected
org.nativescript.widgets.StackLayout{6c8498a V.E..... .....I. 0,0-0,0} to equal
org.nativescript.widgets.StackLayout{390e391 V.E..... .....I. 0,0-768,928}
File "file:///data/data/...../tns-core-modules/ui/tab-view/tab-view.js, line: 64, column: 16

Is there code involved? If so, please share the minimal amount of code needed to recreate the

This is how my tab-view code looks:

<TabView #tabView *ngIf="registers" [selectedIndex]="selectedTabIndex" (selectedIndexChanged)="tabViewIndexChange(tabView.selectedIndex)"
    androidSelectedTabHighlightColor="#f44336">
    <ng-template ngFor [ngForOf]="registers" let-reg>
        <StackLayout *tabItem="{title: reg.name}">
            <kard-board-register [model]="reg"></kard-board-register>
        </StackLayout>
    </ng-template>
</TabView>

So some equality check inside the PageAdapterImpl.destroyItem fails, but I could not yet figure out why. If this cant be resolved I guess I have to roll back to NS 2.5

EDIT
I've tested the issue further: The TabView seems to hold instantiated versions of the current tab and the ones next to the current one (if there is one on each side). Now, when the third tab is selected the instance for the first tab is disposed, hence the call to the destroyItem method. I think this behaviour is related to the NS 3.0 updates regarding view recycling (or rather the new view life cycle).
Yet, unfortunately the instance passed to destroyItem does not equal the item the tab-view class is keeping track of. Furthermore, as already mentioned, the first two tab-items are not getting rendered at all.

The source for the destroyItem method has the following note attached:

// Note: this.owner._removeView will clear item.view.nativeView.
// So call this after the native instance is removed form the container. 
// if (item.view.parent === this.owner) {
//     this.owner._removeView(item.view);
// }

Would it be possible that this call order isn't met?
https://github.com/NativeScript/NativeScript/blob/master/tns-core-modules/ui/tab-view/tab-view.android.ts#L78

EDIT 2
So, I've tested the issue without fetching the models for my tabs from the backend. Binding three tabs directly is working without problems - all tabs are rendered correctly and I am able to switch between them. I even found a fix for making it work with the remote tab models: Instead of the suggested *ngIf="registers" I used *ngIf="registers.length > 0". It solves the issue for the initial display, yet when I add a tab while the tab-view is displayed, the same behaviour occurs again.

Copied from original issue: NativeScript/NativeScript#4380

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions