I don't get why this ClassCastException occurs

后端 未结 5 1882
故里飘歌
故里飘歌 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:14

    Unfortunately there is no way to cast like that for arrays in Java. You will have to iterate your array and cast each object individually.

    The reason for this is type Safety, the JVM simply cannot ensure that the contents of your array can be casted to Uri, without having to iterate thru them, which is why you have to iterate them and cast them individually.

    Basically because Parcelable could be inherited by other Objects, there is no guarantee that the Array contains only Uri objects. However casting to a supertype would work since then type safety would be ok.

    0 讨论(0)
  • 2020-12-09 09:20

    Arrays do have polymorphic behaviour - only generic types don't have.

    That is, if Uri implements Parcelable then

    you CAN say:

    Parcelable[] pa = new Uri[size];
    Uri[] ua = (Uri[]) pa;
    

    you CANNOT say:

    List<Parcelable> pl = new ArrayList<Uri>();
    

    As you see we can cast pa back to Uri[]. Then what is the problem? This ClassCastException happens when your app is killed and later the saved array is recreated. When it is recreated the runtime does not know what kind of array (Uri[]) it was so it just creates a Parcelable[] and puts elements into it. Hence the ClassCastException when you try to cast it to Uri[].

    Note that the exception does not happen (theoretically) when the process is not killed and the originally created array (Uri[]) is reused between state save/restore rounds. Like when you change the orientation.

    I just wanted to make clear WHY it happened. If you want a solution @solo provided a decent one.

    Cheers

    0 讨论(0)
  • 2020-12-09 09:21

    Use this method, it work for me.

    Parcelable[] ps = getIntent().getParcelableArrayExtra();
    Uri[] uri = new Uri[ps.length];
    System.arraycopy(ps, 0, uri, 0, ps.length);
    
    0 讨论(0)
  • 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<parcelables.length; i++) {
                parcelables[i] = new Uri();
            }
    
            return parcelables;
        }
    
        public static Parcelable[] getParcelableArrayExtra() {        
            // The Android system has enough information that it needs to create Uri[]
    
            Uri[] temp = new Uri[3];
            for (int i=0; i<temp.length; i++) {
                temp[i] = new Uri();
            }
            Parcelable[] parcelables = temp;
            return parcelables;
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // OK
            {
                // true
                System.out.println(getParcelableArrayExtra() instanceof Uri[]);
    
                Uri[] uris = (Uri[])getParcelableArrayExtra();
                for (Uri uri : uris) {
                    System.out.println(uri);
                }
            }
    
            // Crash!
            {
                // false
                System.out.println(getParcelableArrayExtraDuringLowMemoryRestoration() instanceof Uri[]);
    
                // ClassCastException.
                Uri[] uris = (Uri[])getParcelableArrayExtraDuringLowMemoryRestoration();         
                for (Uri uri : uris) {
                    System.out.println(uri);
                }
            }
        }    
    }
    

    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<parcelables.length; i++) {
                parcelables[i] = new Uri();
            }
    
            return parcelables;
        }
    
        public static Parcelable[] getParcelableArrayExtra() {        
            // The Android system has enough information that it needs to create Uri[]
            Uri[] temp = new Uri[3];
            for (int i=0; i<temp.length; i++) {
                temp[i] = new Uri();
            }
            Parcelable[] parcelables = temp;
            return parcelables;
        }
    
        private static Uri[] safeCastToUris(Parcelable[] parcelables) {
            if (parcelables instanceof Uri[]) {
                return (Uri[])parcelables;
            }
            int length = parcelables.length;
            Uri[] uris = new Uri[length];
            System.arraycopy(parcelables, 0, uris, 0, length);
            return uris;
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // OK
            {
                Uri[] uris = safeCastToUris(getParcelableArrayExtra());
                for (Uri uri : uris) {
                    System.out.println(uri);
                }
            }
    
            // OK too!
            {
                Uri[] uris = safeCastToUris(getParcelableArrayExtraDuringLowMemoryRestoration());            
                for (Uri uri : uris) {
                    System.out.println(uri);
                }
            }
        }    
    }
    
    0 讨论(0)
  • 2020-12-09 09:30

    I think what's happening is something as follows:

    class Parent { }
    
    class MaleParent extends Parent { }
    
    class FemaleParent extends Parent { }
    

    If the scenario is something as above then the following will fail at runtime:

    Parent[] parents = new FemaleParent[]{};
    MaleParent[] maleParents = (MaleParent[]) parents;
    

    Something as follows does not raise the exception:

    Parent[] parents = new MaleParent[]{};
    MaleParent[] maleParents = (MaleParent[]) parents;
    
    0 讨论(0)
提交回复
热议问题