Java Android:如何使用context作为参数提取方法

时间:2016-04-27 08:52:32

标签: java android android-layout android-fragments android-studio

我正在尝试提取一个将歌曲信息返回给新类的方法,这样我就可以从多个类中调用它的实例。

方法:

public int getSongList(ContentResolver resolver){
        //retrieve song info
        ContentResolver musicResolver = resolver;
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

        if(musicCursor!=null && musicCursor.moveToFirst()){
            //get columns
            int titleColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.ARTIST);
            //add songs to list
            do {
                long thisId         = musicCursor.getLong(idColumn);
                String thisTitle    = musicCursor.getString(titleColumn);
                String thisArtist   = musicCursor.getString(artistColumn);
                songList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (musicCursor.moveToNext());
        }
        return 0;
    }

假设将上述方法放在MainActivity中。这是我试图从片段中实例化它的方式。

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreateView(inflater,container,savedInstanceState);

            View rootView   = inflater.inflate(R.layout.library_songs, container, false);

            this.mView      = rootView;

            songView = (ListView) mView.findViewById(R.id.song_list);
            songList = new ArrayList<Song>();
            new MainActivity().getSongList(mView.getContext().getContentResolver())

            Collections.sort(songList, new Comparator<Song>(){
              public int compare(Song a, Song b){
                return a.getTitle().compareTo(b.getTitle());
            }
            });

            SongAdapter songAdt = new SongAdapter(mView.getContext(), songList);
            songView.setAdapter(songAdt);

            return rootView;
        }

错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.util.ArrayList.add(java.lang.Object)'                                                                              在com.auxiline.audioscope.MainActivity.getSongList(MainActivity.java:219)

1 个答案:

答案 0 :(得分:0)

调用

new MainActivity().getSongList(mView.getContext().getContentResolver())

因为您正在创建MainActivity的新对象,并且在该对象中,songList对象为null。 此外,您似乎在MainActivity中有一个songList副本,而在片段中有另一个副本,因此您必须决定使用一个。 要解决这个问题,您可以将MainActivity的对象传递给片段构造函数中的片段:

public MyFragment(MainActivity activity,...){ .... }

所以当您在MainActivity中创建片段对象时,使用:

MyFragment mMyFragment = new MyFragment(MainActivity.this , ... );

此外,您还必须重写getSongList方法,以使其使用片段中的songList对象:

public int getSongList(ContentResolver resolver,ArrayList<Song> songList)