intent getSerializableExtra ClassCastException

In my app I have this line:

Item item = (Item)getIntent().getSerializableExtra(ITEM_TRANSFER);

The error I get is: Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to com.example.shop.Item

Item Class implements Serializable so I don't know what the problem is.

@Override

public void onItemClick(View view, int position){
    Intent intent = new Intent(this, ItemDetailActivity.class);
    intent.putExtra(ITEM_TRANSFER, mRecyclerViewAdapter.getItemId(position) );
    startActivity(intent);
}

This is how I use Extra

2 answers

  • answered 2018-01-13 17:07 user9209780

    Try next code:

    getIntent().getExtras().getSerializable(ITEM_TRANSFER);
    

  • answered 2018-01-13 17:10 TheCodeStar

    Send Item value instead your passing getItemId()

    @Override
    
    public void onItemClick(View view, int position){
        Intent intent = new Intent(this, ItemDetailActivity.class);
        intent.putExtra(ITEM_TRANSFER, item);
        startActivity(intent);
    }
    

    Receive

    Item item = (Item)getIntent().getSerializableExtra(ITEM_TRANSFER);