splitwise / TokenAutoComplete

Gmail style MultiAutoCompleteTextView for Android
Apache License 2.0
1.3k stars 383 forks source link

ClassCastException when device is rotated #382

Closed iskugor closed 5 years ago

iskugor commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

@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.

Goooler commented 3 months ago

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