I don't get why this ClassCastException occurs

后端 未结 5 1902
故里飘歌
故里飘歌 2020-12-09 08:35
// Application ...
Intent i = new Intent();
i.putExtra(EXTRA_FILE_UPLOAD_URIS, mGalleryAdapter.getItems()); 

Uri[] getItems() { return mItems; }

// Service ...
int         


        
5条回答
  •  忘掉有多难
    2020-12-09 09:22

    https://stackoverflow.com/a/8745966/72437 and https://stackoverflow.com/a/20073367/72437 have well explanation on why such crash happens.

    https://stackoverflow.com/a/14866690/72437 also has a example on how we can workaround with this.

    I would like to provide code examples, to help better understanding.

    Let me demonstrate an example on why such incident fails sometimes.

    An example to demonstrate why such crash happens

    package javaapplication12;
    
    /**
     *
     * @author yccheok
     */
    public class JavaApplication12 {
    
        public static class Parcelable {
    
        }
    
        public static class Uri extends Parcelable {
    
        }
    
        public static Parcelable[] getParcelableArrayExtraDuringLowMemoryRestoration() {    
            // The Android system has no way to know it needs to create Uri[],
            // during low memory restoration process.
    
            Parcelable[] parcelables = new Parcelable[3];
    
            for (int i=0; i

    An example to demonstrate on how we can fix this

    package javaapplication12;
    
    /**
     *
     * @author yccheok
     */
    public class JavaApplication12 {
    
        public static class Parcelable {
    
        }
    
        public static class Uri extends Parcelable {
    
        }
    
        public static Parcelable[] getParcelableArrayExtraDuringLowMemoryRestoration() {    
            // The Android system has no way to know it needs to create Uri[],
            // during low memory restoration process.
            Parcelable[] parcelables = new Parcelable[3];
    
            for (int i=0; i

提交回复
热议问题