miteshpithadiya/SearchableSpinner

When the listview dialog is opened and I press the home button, the app crashes. How can I fix that?

Opened this issue · 2 comments

When the listview dialog is opened and I press the home button, the app crashes. How can I fix that?

I noticed the same issue. Also happens when hitting the "recent apps" button. Interestingly, does not happen on orientation change. The cause (when I encountered it) is thus:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.toptoche.searchablespinnerlibrary.SearchableSpinner)

which is in turn caused by, in my case:

Caused by: java.io.NotSerializableException: android.widget.ArrayAdapter

So the issue seems to be attempting to serialize the adapter attached to the "Spinner," if said adapter is not serializable. Without changing the source code of the project, the only fixes off the top of my head are:

  • to manually de-allocate your Adapter in onSaveInstanceState, save the contents in some other way (like a static List, perhaps), then restore the Adapter in onRestoreInstanceState.
  • to create your own Serializable Adapter that subclasses SpinnerAdapter, and use that to attach to this.

A pain? Yes, and so I personally will probably look for another solution unless this (and other things) are fixed.

If you simply need a general (no specific details) Searchable Spinner implementation, I also found this one:

https://github.com/michaelprimez/searchablespinner

This one requires (currently) targeting API 21+, however.

I encountered this error in my app, but I solved this by looking at the error.
I'll tell you the solution to the problem in short.
I was using ArrayAdapter in following way
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.whatever));
to display spinner items, but while initializing ArrayAdapter, you can see I made an unchecked call there, so my app crashed when it was minimized.
So what I changed in this code was adding concrete type to the object by modifying it in following way
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.whatever));
Now, as my spinner only contained Strings, I could pass String there, but, depending on the layout you'll be submitting to the spinner(if you're using custom views) you can pass your POJO class there.