Question

I have this code

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.offers_list);

    Intent callingIntent = getIntent();
    if (callingIntent != null) {
        Bundle extras = callingIntent.getExtras();
        if (extras != null) {
            Offer[] offers = (Offer[]) extras
                    .getSerializable(PublicMacros.OFFERS_LIST);
        }
    }

and i get this error after executing

02-19 01:59:52.291: E/AndroidRuntime(26440): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.zoomer.offers.Offer[]

how can i cast properly?

Was it helpful?

Solution

It isn't possible to cast an object[] to a T[] where T is an abitrary class.

But you can use

Offer[] array = Arrays.copyOf(a, a.length, Offer[].class);

Where a is the returned array from

extras.getSerializable(PublicMacros.OFFERS_LIST);

Or you can iterate over each element in the array and cast each one for its own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top