Yong Jun
Yong Jun

Reputation: 9

My app keeps crashing when I intent from a Fragment to a new Activity (Android)

I am having trouble with fragments since I'm new to it. My problem now is that my app keeps crashing whenever I intent from a fragment to another activity. Also, one of my functions is not working and usable. If anyone can help me, it would be very much appreciated.

public class LibraryFragment extends Fragment {

//Create a new SongCollection variable
private SongCollection activateCollection = new SongCollection();

//This is the place where all the execution happens
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Make sure to put this statement for the last
    return inflater.inflate(R.layout.fragment_library, container, false);
}

//When the user taps on the cover arts
public void handleSelection(View view){

    String resourceId = AppUtil.getResourceId(getActivity(), view);

    Song selectedSong = activateCollection.searchById(resourceId);

    AppUtil.popMessage(getActivity(), "Now playing: " + selectedSong.getTitle());

    sendDataToActivity(selectedSong);

}

//A method which directs user to the "Now Playing" page as well as transfer songs information to the next activity
public void sendDataToActivity(Song track){

    //An intent which directs user to the next activity
    Intent toNowPlayingPage = new Intent(getActivity(), PlaySongActivity.class);

    //Store the song information to be sent over to the next page
    toNowPlayingPage.putExtra("id", track.getId());
    toNowPlayingPage.putExtra("title", track.getTitle());
    toNowPlayingPage.putExtra("artist", track.getArtist());
    toNowPlayingPage.putExtra("fileLink", track.getFileLink());
    toNowPlayingPage.putExtra("coverArt", track.getCoverArt());

    //Initiate the intent
    startActivity(toNowPlayingPage);
}

}

Upvotes: 0

Views: 61

Answers (1)

Muhammad Umair
Muhammad Umair

Reputation: 273

inflate the layout on View, And pass View object on called method handleSelection(view); in onCreate(). Because you did't called the Method handleSelection(); and there is no onClicklistener on View

Upvotes: 3

Related Questions