public class Adpter、onCreateViewHolder、MediaPlayer 出现 NullPointerException

NullPointerException at public class Adpter, onCreateViewHolder, MediaPlayer

我正在将数据传递到我的 recyclerview 适配器

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

    }

}

但是我一次得到 NUll 指针异常

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

问题就出在这里。您正在尝试在实际为 SongUri 赋值之前创建一个 MediaPlayer 实例。

在为SongUri 赋值后,在setData() 函数中创建MediaPlayer 实例。请对您的代码进行以下更改,它应该可以正常工作。

    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
    }