I don't get why this ClassCastException occurs

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

提交回复
热议问题