I am passing data to my recyclerview adpter
RecyclerViewAdpter
package com.example.alarm.Apters;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.alarm.ExtraClass.External_Stotrage;
import com.example.alarm.Models.RingtoneModel;
import com.example.alarm.R;
import java.util.ArrayList;
public class Ringtone_Adpter extends RecyclerView.Adapter<Ringtone_Adpter.ViewHolder> {///24 Ist NullPointer exception
ArrayList<RingtoneModel> list;
Context context;
Uri finalUri_send;
public Ringtone_Adpter(ArrayList<RingtoneModel> list, Context context) {
this.list = list;
this.context = context;
}
public Uri getFinalUri_send() {
return finalUri_send;
}
public Ringtone_Adpter(){}
@NonNull
@Override
public Ringtone_Adpter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.ringtone_lyt_res_file,parent,false);
return new ViewHolder(view); ///45 2nd NullPointer exception
}
@Override
public void onBindViewHolder(@NonNull Ringtone_Adpter.ViewHolder holder, int position) {
String ringtonename = list.get(position).getSongName();
Uri songuri = Uri.parse(list.get(position).getUri());
holder.setData(ringtonename,songuri);
this.finalUri_send = holder.getUriforalarmoutside();
}
@Override
public int getItemCount() {
Log.d("Length TAG", String.valueOf(list.size()));
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView play_pause_icon,Volume_icon;
TextView Ringtone_name,RingLength;
Button Set_btn;
SeekBar seekBar;
Uri SongUri = null;
Uri Uriforalarm;
public void setData(String ringtonename, Uri songuri) {
Ringtone_name.setText(ringtonename);
this.SongUri = songuri;
}
public ViewHolder(@NonNull View itemView) {
super(itemView);
play_pause_icon = itemView.findViewById(R.id.play_pause_icon);
Volume_icon = itemView.findViewById(R.id.Volume_icon);
Ringtone_name = itemView.findViewById(R.id.Ringtone_name);
Set_btn = itemView.findViewById(R.id.Set_btn);
RingLength = itemView.findViewById(R.id.RingLength);
seekBar = itemView.findViewById(R.id.seekBar);
// TODO: Actual code
MediaPlayer mediaPlayer = MediaPlayer.create(context,SongUri);//83 3rd NullPointer exception
RingLength.setText(mediaPlayer.getDuration());
play_pause_icon.setOnClickListener(v ->{
if (!mediaPlayer.isPlaying()){
play_pause_icon.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
Volume_icon.setVisibility(View.INVISIBLE);
mediaPlayer.start();
}else {
play_pause_icon.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
Volume_icon.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
});
Set_btn.setOnClickListener(v ->{
Uriforalarm = SongUri;
new External_Stotrage().SavePrivatly(context,Uriforalarm,"Ring For Ringing");
});
}
final public Uri getUriforalarmoutside(){
return Uriforalarm;
}
}
}
but I am getting NUllpointerexception at a time
java.lang.NullPointerException
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:889)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:874)
at android.media.MediaPlayer.create(MediaPlayer.java:808)
at android.media.MediaPlayer.create(MediaPlayer.java:789)
at com.example.alarm.Apters.Ringtone_Adpter$ViewHolder.<init>(Ringtone_Adpter.java:89) //at line 89
at com.example.alarm.Apters.Ringtone_Adpter.onCreateViewHolder(Ringtone_Adpter.java:45) //at line 45
at com.example.alarm.Apters.Ringtone_Adpter.onCreateViewHolder(Ringtone_Adpter.java:24) //at line 24
line 89 == MediaPlayer mediaPlayer = MediaPlayer.create(context,SongUri);//83
line 45 == return new ViewHolder(view); //45
line 24 === public class Ringtone_Adpter extends RecyclerView.Adapter<Ringtone_Adpter.ViewHolder> { //24
MediaPlayer mediaPlayer = MediaPlayer.create(context,SongUri);//83 3rd NullPointer exception
This is where the problem lies. You are trying to create a MediaPlayer Instance before actually assigning value to SongUri.
Create MediaPlayer instance in setData() fuction after assigning value to SongUri. Please make below changes in your code and it should work fine.
public void setData(String ringtonename, Uri songuri) {
Ringtone_name.setText(ringtonename);
this.SongUri = songuri;
MediaPlayer mediaPlayer = MediaPlayer.create(context,SongUri);//83 3rd NullPointer exception
RingLength.setText(mediaPlayer.getDuration());
play_pause_icon.setOnClickListener(v ->{
if (!mediaPlayer.isPlaying()){
play_pause_icon.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
Volume_icon.setVisibility(View.INVISIBLE);
mediaPlayer.start();
}else {
play_pause_icon.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
Volume_icon.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
});
Set_btn.setOnClickListener(v ->{
Uriforalarm = SongUri;
new External_Stotrage().SavePrivatly(context,Uriforalarm,"Ring For Ringing");
});
}
public ViewHolder(@NonNull View itemView) {
super(itemView);
play_pause_icon = itemView.findViewById(R.id.play_pause_icon);
Volume_icon = itemView.findViewById(R.id.Volume_icon);
Ringtone_name = itemView.findViewById(R.id.Ringtone_name);
Set_btn = itemView.findViewById(R.id.Set_btn);
RingLength = itemView.findViewById(R.id.RingLength);
seekBar = itemView.findViewById(R.id.seekBar);
// TODO: Actual code
}