1

I'm developing android music player and its my first app but there is something wrong in my code hence getting MainActivity(fragment tabs,ViewPager,naginational drawer code) can not cast PlayerActivity(music player ui,playing related stuff ) calling my player activity from songs Fragment.

My SongsFragment Code :

@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;

Player Activity Code has OnCreate Method ., also if anyone help me sorting songs list as album/artist . thanks

Coolalien
  • 49
  • 1
  • 1
  • 7
  • Where is `MainActivity(fragment tabs,ViewPager,naginational drawer code)` line in code? show relevant part of code – ρяσѕρєя K Jan 06 '16 at 14:26
  • i mean i've added all those things in MainActivity....PlayerActivity want to load from songs fragment.....its causing due to Oncreate method that is in MainActivity and PlayerActivity....so is it possible to load Player Activity after MainActivity's on create Method ?? – Coolalien Jan 06 '16 at 14:30
  • Fragment can bellow only to one activity, so if you created it from MainActivity you won't be able to access to PlayerActivity – gio Jan 06 '16 at 18:49
  • thanks @gio ....i've solved by moving from player activity to main activity – Coolalien Jan 07 '16 at 07:14

0 Answers0