Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException when device is rotated #382

Closed
iskugor opened this issue Apr 16, 2019 · 6 comments
Closed

ClassCastException when device is rotated #382

iskugor opened this issue Apr 16, 2019 · 6 comments

Comments

@iskugor
Copy link

iskugor commented Apr 16, 2019

Hello.

I'm using latest 3.0.1 version.

When device is rotated, app crashes with logs:

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
        at com.tokenautocomplete.TokenCompleteTextView.reifyParameterizedTypeClass(TokenCompleteTextView.java:1337)
        at com.tokenautocomplete.TokenCompleteTextView.onSaveInstanceState(TokenCompleteTextView.java:1360)
        at android.view.View.dispatchSaveInstanceState(View.java:18516)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3802)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3802)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3802)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3802)
        at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3802)
        at android.view.View.saveHierarchyState(View.java:18499)
        at com.android.internal.policy.PhoneWindow.saveHierarchyState(PhoneWindow.java:2100)
        at android.app.Activity.onSaveInstanceState(Activity.java:1622)

Any idea how to fix this?

Thanks.

@mgod
Copy link
Contributor

mgod commented Apr 18, 2019

The problem is in the logic to save and restore the instance state. What class are you setting as the type for the TokenCompleteTextView subclass? I'm guessing that the class type you're setting isn't playing nice with the default save and restore instance state code.

Probably the solution will be for you to implement these yourself as described here: https://github.com/splitwise/TokenAutoComplete#restoring-the-view-state

@nrazon
Copy link

nrazon commented Apr 22, 2019

I have similar problem with latest version (v3.0.1).
When onPause fires and onsaveInstance called on TokenCompleteTextView, it throws same error.
I am using ContactsCompletionView sample you provided on readme to extend for TokenCompleteTextView.
My model is seralizable/parcelable but still I try to implement convertSerializableObjectsToTypedObjects & getSerializableObjects, still it is throwing same error.When I check code flow this crash occures before this functions fires on reifyParameterizedTypeClass line 1337.

@mgod
Copy link
Contributor

mgod commented Apr 23, 2019

Edit: this diagnosis is incorrect, the issue was that the TokenCompleteTextView type parameter was not set.

Ok, so the core of the problem is that calling getClass on whatever object is getting hit by this is returning a class that is not a subclass (at any level of remove) of TokenCompleteTextView. I suspect something is happening to make your system use a reference to a raw TokenCompleteTextView instead of your subclass somehow. Is there any place in your code that you directly reference your view as a TokenCompleteTextView instead of your subclass? Maybe in the layout?

I'm not able to reproduce this in the sample app, though this might be an app, keyboard or OS version issue. Can you give me those details?

@nrazon
Copy link

nrazon commented Apr 24, 2019

@mgod I solved why it happens.

My class which extends TokenCompleteTextView was like this:
public class ContactsCompletionView extends TokenCompleteTextView

I wasn't passing my model while extending TokenCompleteTextView, It was working with V2.

Now I reaalized that code piece which throws error needs to know my model class. So while extending I am passing my model as well as your sample:
public class ContactsCompletionView extends TokenCompleteTextView<ModelRelationChips>

And all problems gone away.

Thanks for help.

@mgod
Copy link
Contributor

mgod commented Apr 24, 2019

@nrazon Thanks for the follow-up. I'm going to close this for now, but I know this kind of issue doesn't pop up until you've had to save instance state, so I'll probably look into seeing if I can surface an error message/exception for this sooner or work around this specific case.

@mgod mgod closed this as completed Apr 24, 2019
@Goooler
Copy link
Contributor

Goooler commented Aug 31, 2024

This should be duplicated with #449 if you were enabling R8 fullMode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants