Nativescript: Android 'Wrong state class, expecting View State...'

Created on 7 Feb 2017  路  7Comments  路  Source: NativeScript/NativeScript

Please, provide the details below:

Did you verify this is a real problem by searching Stack Overflow and the other open issues in this repo?

Yes

Tell us about the problem

Only in Android, the app crashes when I have 2 different elements (in my case, a MapView which comes from the nativescript-google-maps-sdk plugin, and a ListView) in the same row and column in a Page (A) with a GridLayout, then I navigate to a different Page (B), then I navigate back to A.

Which platform does your issue occur on?

Android, only found on version 4.4.4.

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

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

  1. tns run android using Android 4.4.4 device or emulator.
  2. Tap any list item or tap the Go to detail button.
  3. Navigate back (by pressing the hardware 'back' button, or the navigation button).
  4. App crashes. The console outputs nothing, but the ErrorActivity produces this:

An uncaught Exception occurred on "main" thread.
java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.AbsListView$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/0x2. Make sure other views do not use the same id.
at android.view.View.onRestoreInstanceState(View.java:13903)

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

Project is here.

bug android

Most helpful comment

I was able to fix this issue (I think) by adding the following:

app/App_Resources/Andorid/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="my_list_view" type="id" />
</resources>

map.component.html:

    <ListView row="1" [items]="myItems" (itemTap)="onItemTapped($event)" (loaded)="onListViewLoaded($event)">

map.component.ts:

onListViewLoaded(event) {
    if (event.object.android) {
        event.object.android.setId(org.nativescript.mapview.R.id.my_list_view);
    }
}

The issue could have been caused by this, but I'm not certain:

tns-core-modules/ui/list-view/list-view.android.ts:

public _createNativeView() {
    ...
    if (this._androidViewId < 0) {
        this._androidViewId = android.view.View.generateViewId();
    }
    this._android.setId(this._androidViewId);
    ...
}

So far, I haven't seen any repercussions from manually setting the id in my project.

Updated project and resolution is here.

All 7 comments

I even tried alleviating the problem by adding

ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="my_map_view" type="id" />
    <item name="my_list_view" type="id" />
</resources>

and

<MapView #mapView row="1" (mapReady)="onMapReady($event)" android.id="R.id.my_map_view"></MapView>

<ListView row="1" [items]="myItems" (itemTap)="onItemTapped($event)"
    android.id="R.id.my_list_view">

Hi @EricRobertBrewer,
Thank you for reporting this issue.
I confirm this is a real issue for Android while using nativescript-google-maps-sdk and ListView. This problem could be reproduced, when navigating back to a page, with Google maps.

This behavior has been reproduced also on non NativeScirpt Angular 2 project. I am attaching a link to sample project, where the problem could be reproduced.

I'm also having this issue as well. Only occurs when listview is on html page that belongs to map component. Solution for anybody having this problem temporarily...put listview elsewhere or remove it until this is fixed.

I was able to fix this issue (I think) by adding the following:

app/App_Resources/Andorid/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="my_list_view" type="id" />
</resources>

map.component.html:

    <ListView row="1" [items]="myItems" (itemTap)="onItemTapped($event)" (loaded)="onListViewLoaded($event)">

map.component.ts:

onListViewLoaded(event) {
    if (event.object.android) {
        event.object.android.setId(org.nativescript.mapview.R.id.my_list_view);
    }
}

The issue could have been caused by this, but I'm not certain:

tns-core-modules/ui/list-view/list-view.android.ts:

public _createNativeView() {
    ...
    if (this._androidViewId < 0) {
        this._androidViewId = android.view.View.generateViewId();
    }
    this._android.setId(this._androidViewId);
    ...
}

So far, I haven't seen any repercussions from manually setting the id in my project.

Updated project and resolution is here.

Thnx @EricRobertBrewer i've come across this problem and this helped me fix it.

I'm using id's for appium testing, any update on how to set Id's on elements hybrid xml for android? is it at all possible

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings