0

I'm creating a feature for my app where a playlist is displayed and when the user clicks on the thumbnail of a video, a new activity opens which displays the video in a fragment. However the videoId doesnt seem to pass through the intent because whenever I click on the error that the bundle seems to be null. "Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference"

This is my adapter class where the onClickListener is.

    public class Adapter extends RecyclerView.Adapter<ViewHolder> {

    Context context;
    Items[] items;

    public Adapter(Context context, Items[] items) {
        this.context = context;
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.textView.setText(items[position].getSnippet().getTitle());
        Picasso.get().load(items[position].getSnippet().getThumbnails().getHigh().getUrl()).into(holder.imageView);

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ActivityVideoDetails.class);
                intent.putExtra(("videoId"), items[position].getContentDetails().getVideoId());
//                Log.d("TAG",items[position].getContentDetails().getVideoId());
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return items.length;
    }
}

There error appears to be from the onCreate class where the bundle is null public class

VideoFragment extends YoutubePlayerSupportFragmentX implements YouTubePlayer.OnInitializedListener {

    private OnVideoPlayListener onVideoPlayListener;

    public interface OnVideoPlayListener {
        void onPlaying(String videoId);
    }

    String videoId;

    public VideoFragment() {

    }

    @Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        final Bundle arguments = getArguments();

        if(bundle!=null && bundle.containsKey("KEY_VIDEO_ID")){
            videoId = bundle.getString("KEY_VIDEO_ID");
        }
        else {
            videoId = arguments.getString("KEY_VIDEO_ID");
        }

        initialize("youtubeAPI", this);
    }

    public void setVideoId(final String mVideoId) {
        videoId = mVideoId;
        initialize("youtubeAPI", this);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        if(videoId !=null){
            if(b){
                youTubePlayer.play();
            }
            else {
                youTubePlayer.loadVideo(videoId);
            }

            if(onVideoPlayListener!= null){
                onVideoPlayListener.onPlaying(videoId);
            }
        }

    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        if(youTubeInitializationResult.isUserRecoverableError()) {
            youTubeInitializationResult.getErrorDialog(getActivity(), 1).show();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle bundle){
        super.onSaveInstanceState(bundle);
        bundle.putString("KEY_VIDEO_ID", videoId);
    }
}

Here is my ActivityDetails class which calls onto the fragment

public class ActivityVideoDetails extends AppCompatActivity {

    String videoId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_details);

        getData();
        playVideo();
    }

    private void getData() {
        Intent intent = getIntent();

        videoId = intent.getStringExtra("videoId");
    }

    private void playVideo(){
        final YouTubeInitializationResult result = YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(this);

        if(result!= YouTubeInitializationResult.SUCCESS){
            result.getErrorDialog(this, 0).show();
            return;
        }

        final VideoFragment videoFragment = (VideoFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fargment_youtube);
        videoFragment.setVideoId(videoId);
    }
}
tza00
  • 97
  • 1
  • 9
  • Why is there TWO "Bundle" objects , one you make in the method and more than that is dangerous as final. And the other is a Bundle object belonging to the super(Bundle bundle) class. Neither getString method has a code implementation but when called are not associate any defined calling object e.g. "scope" super.getString() if you want the super class bundle if the super is abstract and or not public. – Samuel Marchant Jan 12 '22 at 02:28
  • You are passing the bundle to `ActivityVideoDetails` and trying to access it in `VideoFragment`, does `ActivityVideoDetails` do anything with it (pass it on to the Fragment)? – Tyler V Jan 12 '22 at 03:04
  • yes activity details passes on to the fragment. – tza00 Jan 12 '22 at 03:10
  • Probably good to add the details of how it does that to the question. Do you use `fragment.setArguments(bundle);`? – Tyler V Jan 12 '22 at 03:13
  • sorry I think I misunderstood what you said, ActviityDetails doesnt pass on the bundle. – tza00 Jan 12 '22 at 03:19
  • Ok, if you are not passing in the bundle then you can't access the string in the Fragment from the `getArguments()` bundle. The [arguments method](https://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment) requires you to call `setArguments` on the fragment instance to pass in the data. You can probably just remove the fragment `onCreate` method you have entirely. – Tyler V Jan 12 '22 at 03:19

0 Answers0