0

I hope you who is reading can shed from light here :)

I am having a problem when I close my app from an Activity, I get this error:

java.lang.RuntimeException: Parcel: unable to marshal value

Background: I am saving the spinner adapters data in a List< SpinnerItem> in the onSaveInstanceState() method and restoring it in the onRestoreInstanceState() which works perfectly with no problems.

However, when I come to exit the app it obviously fires off the onSaveInstanceState() and then I get the parcelable error. I have tried commenting out the code that saves the List in the bundle and this stops the error from appearing.

Clear it's to do with the List but I cannot understand why or find a way around it.

Heres the code

onSaveInstanceState() -

super.onSaveInstanceState( savedInstanceState );

if ( loginResponse != null )
{
  savedInstanceState.putSerializable( SAVE_LOGIN_RESPONSE, loginResponse );
}
//Company states
List<SpinnerItem> companySpinnerList = getSpinnerAdapterAsList( spinner_setupCompany.getAdapter() );
if ( companySpinnerList != null )
{
  savedInstanceState.putSerializable( SAVE_COMPANY_LIST, (java.io.Serializable) companySpinnerList );
}
savedInstanceState.putInt( SAVE_COMPANY_SELECTION, spinner_setupCompany.getSelectedItemPosition() );
savedInstanceState.putBoolean( SAVE_COMPANY_ENABLED, spinner_setupCompany.isEnabled() );
//Depot states
List<SpinnerItem> depotSpinnerList = getSpinnerAdapterAsList( spinner_setupDepot.getAdapter() );
if ( depotSpinnerList != null && depotSpinnerList.size() > 1 )
{ //great than 1 because the first value is empty
  savedInstanceState.putSerializable( SAVE_DEPOT_LIST, (java.io.Serializable) depotSpinnerList );
}
savedInstanceState.putInt( SAVE_DEPOT_SELECTION, spinner_setupDepot.getSelectedItemPosition() );
savedInstanceState.putBoolean( SAVE_DEPOT_ENABLED, spinner_setupDepot.isEnabled() );
// Save view states
savedInstanceState.putBoolean( SAVE_VIEW_ENABLED_SERVER, editText_serverName.isEnabled() );
savedInstanceState.putBoolean( SAVE_VIEW_ENABLED_USERNAME, editText_setupUsername.isEnabled() );
savedInstanceState.putBoolean( SAVE_VIEW_ENABLED_PASSWORD, editText_setupPassword.isEnabled() );
savedInstanceState.putBoolean( SAVE_BUTTON_ENABLED_LOGIN, button_login.isEnabled() );

getSpinnerAdapterAsList() -

final List<SpinnerItem> list = new ArrayList<SpinnerItem>();

for ( int i = 0; i < adapter.getCount(); i++ )
{
  list.add( (SpinnerItem) adapter.getItem( i ) );
}

return list;

Thanks very much, have a great day!

:)

Sambuxc
  • 425
  • 2
  • 11
  • 26

1 Answers1

0

First I advise you to use Parcelable instead of Serializable (it is faster):

savedInstanceState.putParcelable(SAVE_DEPOT_LIST,<your list>);

Second, your list is not Parcelable so Create a calss that implment Parcelable

see this link

Community
  • 1
  • 1
  • I'm getting a ClassCastException now - java.util.ArrayList cannot be cast to android.os.Parcelable. How else could I save my SpinnerItem's that would work with parcelables? – Sambuxc Jun 26 '14 at 15:45