zoukankan      html  css  js  c++  java
  • 当在类中的 Parcelable 接口使用 ArrayList < customObject > android 系统: nullPointerException

    我想使调用音乐使用 parcelable,所以我可以访问两个不同的活动中的音乐的实例的类。我不想使用可序列化的速度的目的。当我尝试将它使用传递时,我总是收到 nullPointerException:

    Intent in = getIntent();
    thisInstance = (Music) in.getExtras().get("MusicInstance");
    

    音乐班: (ArrayList 是在底部的顶部和 parcelable 类)

    public class Music implements Parcelable{
    
        private static ArrayList<genericSongClass> songs = new ArrayList<genericSongClass>();
    
        Cursor cursor;
    
        static Context context;
    
        public Music(Context context){
            this.context = context;
        }
    
        public Music(Parcel in){
            in.readTypedList(songs, genericSongClass.CREATOR);
        }
    
        public void BindAllSongs() {        
                /** Making custom drawable */
                String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
                final String[] projection = new String[] {
                        MediaStore.Audio.Media.DISPLAY_NAME,
                        MediaStore.Audio.Media.ARTIST,
                        MediaStore.Audio.Media.DATA,
                        MediaStore.Audio.Media.ALBUM};
                        final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
                                + " COLLATE LOCALIZED ASC";
    
                        try {
                            // the uri of the table that we want to query
                            Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                            // query the db
                            cursor = context.getContentResolver().query(uri,
                                    projection, selection, null, sortOrder);
                            if (cursor != null) {
                                songs = new ArrayList<genericSongClass>(cursor.getCount());
                                cursor.moveToFirst();                       
                                while (!cursor.isAfterLast()) { 
                                    genericSongClass GSC = new genericSongClass();
                                    GSC.songTitle = cursor.getString(0);
                                    GSC.songArtist = cursor.getString(1);   
                                    GSC.songData = cursor.getString(2);
                                    GSC.songAlbum = cursor.getString(3);
                                    songs.add(GSC);
                                    cursor.moveToNext();
                                }
                            }
                        } catch (Exception ex) {
    
                        } finally {
                            if (cursor != null) {
                                cursor.close();
                            }
                        }       
    
            }
    
        public static Object[] toArray(ArrayList<Object> list){
            Object[] toReturn = new Object[list.size()];
            for (int i = 0; i < list.size(); i++){
                toReturn[i] = list.get(i);
            }
            return toReturn;
        }
    
        public ArrayList<String> getArtists(){
            ArrayList<String> artists = new ArrayList<String>();
            for(genericSongClass gsc: songs){
                if(!artists.contains(gsc.songArtist)){
                    artists.add(gsc.songArtist);
                }
            }
            Alphabetize forArtists = new Alphabetize(artists);
            return forArtists.getSortedArrayList();
        }
    
        public ArrayList<String> getAlbums(String artist){
            ArrayList<String> albums = new ArrayList<String>();
            for(genericSongClass gsc: songs){
                if(gsc.songArtist == artist){
                    albums.add(gsc.songAlbum);
                }
            }
            Alphabetize forAlbums = new Alphabetize(albums);
            return forAlbums.getSortedArrayList();
        }
    
        //--- Parcel ------------------------------------------------
    
         public static final Parcelable.Creator<Music> CREATOR = new Parcelable.Creator<Music>() {  
    
                public Music createFromParcel(Parcel in) {  
                    return new Music(in);  
                }
    
                @Override
                public Music[] newArray(int size) {
                    return new Music[size];
                }  
            };
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeList(songs);
        }
    
    
    
    }
    

    genericSongClass:

    public class genericSongClass implements Parcelable {
        String songTitle = "";
        String songArtist = "";
        String songData = "";
        String songAlbum = "";
    
        public genericSongClass(){};
    
        private genericSongClass(Parcel in){
            songTitle = in.readString();
            songArtist = in.readString();
            songData = in.readString();
            songAlbum = in.readString();
        }
    
        public static final Parcelable.Creator<genericSongClass> CREATOR = new Parcelable.Creator<genericSongClass>() {
    
            @Override
            public genericSongClass createFromParcel(Parcel source) {
                return new genericSongClass(source);
            }
    
            @Override
            public genericSongClass[] newArray(int size) {
                return new genericSongClass[size];
            }
    
        };
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(songTitle);
            dest.writeString(songArtist);
            dest.writeString(songData);
            dest.writeString(songAlbum);
        }
    }
    

    为什么我得到 nullpointer 和如何作出补救这种情况吗?

    这是我第一次使用 Parcelable,所以任何的帮助建议更是深受欢迎。先谢谢了!

    解决方法 1:

    到 constructor.use 请以下代码初始化您的列表。

    public Music(Parcel in){
            songs = new ArrayList<genericSongClass>();
            in.readTypedList(songs, genericSongClass.CREATOR);
        }
    

    和获得与此代码:

    thisInstance = getIntent().getParcelableExtra("MusicInstance");
    

    请确保您初始化和 thisInstance 。

  • 相关阅读:
    情感日记:离校,漂流他乡
    汉化破解:{smartassembly}使用指南
    金融市场:Open.Yale.course:Financial.Markets.07.Chi_Eng
    【转载】[解决系统服务运行应用程序的权限问题]使用WTSGetActiveConsoleSessionId()的VISTA服务与桌面交互
    【转载】关于sqlserver自增长列的问题
    【转载】如何给IIS添加能访问的文件类型
    【原创】使用反射之后,强制类型转化不成功的问题在哪?
    【转载】网站开发人员应该知道的61件事
    【索引】转载关于DSL、代码生成器使用、依赖注入方式
    【原】使用SoundPlayer播放wav文件时产生杂音如何处理
  • 原文地址:https://www.cnblogs.com/exmyth/p/4534627.html
Copyright © 2011-2022 走看看