• 当在类中的 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 。

  • 相关阅读:
    AFNet3.0上传图片
    最新 AFNetworking 3.0 简单实用封装
    iOS开发密码输入数字和字母混合
    IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)(转)
    iOS开发探索-图片压缩处理
    常用第三方框架插件
    2.1创建直线
    1.4用向导创建Hello,world程序
    vs2008找不到ObjectARX MFC Support
    vc6.0错误:error C2653: 'CCreateEnt' : is not a class or namespace name
  • 原文地址:https://www.cnblogs.com/exmyth/p/4534627.html
Copyright © 2020-2023  润新知