Android - 无法从片段中转换被调用的活动

时间:2016-01-06 14:18:46

标签: android android-fragments casting main-activity

我正在开发Android音乐播放器及其我的第一个应用程序但是我的代码出现了问题,因此获得MainActivity(片段标签,ViewPager,naginational抽象代码)无法投射播放器活动(音乐播放器ui,播放相关内容) ) 从歌曲Fragment中调用我的播放器活动。

我的歌曲片段代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_songs, container, false);
    songList = ((PlayerActivity) getActivity()).getSongs();

    songView = (RecyclerView) rootView.findViewById(R.id.song_list);
    songView.setHasFixedSize(true);
    GridLayoutManager grid = new GridLayoutManager(getActivity(), 2);
    grid.setOrientation(LinearLayoutManager.VERTICAL);
    songView.setLayoutManager(grid);
    getSongList();
    // sort the tracks
    Collections.sort(songList, new Comparator<Song>() {
        public int compare(Song a, Song b) {
            return a.getTitle().compareTo(b.getTitle());
        }
    });
    ((PlayerActivity) getActivity()).setSongs(songList);
    SongAdapter songAdt = new SongAdapter(songList, getActivity());
    songView.setAdapter(songAdt);
    songView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {

        @Override
        public void onItemClick(View view, int position) {
            MusicService musicService = ((PlayerActivity) getActivity()).getMusicService();
            musicService.setSong(position);
            musicService.togglePlay();
        }
    }));

    return rootView;

播放器活动代码具有OnCreate方法。, 如果有人帮我分类歌曲列表作为专辑/艺术家。 感谢

0 个答案:

没有答案