MediaRecorder.stop() Returns NullPointerException

Asked 1 years ago, Updated 1 years ago, 88 views

Currently, we start recording by pressing the Start button on the dialog, and when we press the Stop button, another save confirmation dialog is displayed, and when we press the Save button, the recorded sound is saved.However, when you finish recording, NullPointerException is returned in mRecorder.stop() within the stopRecording method of the RecDialog class.I tried various things, but the result is still the same.Why is this? Is there anything to do with DialogFragment?

I'm sorry, but I appreciate your cooperation.

RecDialog:

public class RecDialog extensions DialogFragment{
    private MediaRecorder mRecorder;

    @ Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater=(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.rec_dialog, null);

        final ImageButton recordButton=(ImageButton)mView.findViewById(R.id.recButton);
        recordButton.setOnClickListener(newView.OnClickListener(){
            @ Override
            public void onClick (View v) {
                if(!mRecButtonClick){
                    recordButton.setImageResource(R.drawable.microphone_on);
                    mRecorder = new MediaRecorder();
                    startRecording();
                    mRecButtonClick=true;
                } else{
                    recordButton.setImageResource(R.drawable.microphone_off);
                    mRecButtonClick=false;
                    showDialogBroadcast();
                }
        });
    }

    public class MyBroadcastReceiver extensions BroadcastReceiver{
        @ Override
        public void onReceive (Context context, Intent) {
            DialogFragment dialog = new SaveDialog();
            dialog.show(getFragmentManager(), "dialog");
        }
    }

    public void showDialogBroadcast(){
        Intent broadcastIntent= new Intent();
        broadcastIntent.setAction("show_saveDialog");
        getActivity().sendBroadcast(broadcastIntent);
    }

    public void startRecording() {
        File sampleDir=new File(Environment.getExternalStorageDirectory()+"/Test");
        try{
            mAudiofile=File.createTempFile("voice", ".3gp", sampleDir);
        } catch(IOExceptione){
            return;
        }
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource (MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat (MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(mAudiofile.getAbsolutePath());
        try{
            mRecorder.prepare();
        } catch(IOExceptione){
            e.printStackTrace();
        }
        mRecorder.start();
    }

    public void stopRecording (String fileName) {
        mRecorder.stop();
        mRecorder.release();
        addVoiceToMediaLibrary (fileName);
    }

SaveDialog:

RecDialog recDialog=newRecDialog();
recDialog.stopRecording(editText.getText().toString());

android java nullpointerexception

2022-09-30 20:32

1 Answers

As for the content of the question + the questioner's comment, isn't the code structure generally as follows?

class RecDialog{
  private MediaRecorder mRecorder;//<--- instance variable
  //...

  public void startRecording() {
    mRecorder = new MediaRecorder();
    // ...
    mRecorder.start();
  }

  public void stopRecording (String fileName) {
    mRecorder.stop();
    // ...
  }
}

classSaveDialog {
  //...

  public void startOperation(){
    RecDialog recDialog = new RecDialog(); //(1)
    recDialog.startRecording();
  }

  public void stopOperation(){
    RecDialog recDialog = new RecDialog(); //(2)
    recDialog.stopRecording(editText.getText().toString());
  }
}

If mRecorder is the インスタinstance variable 」 of the RecDialog class, the mRecorder drops the instance of the RecDialog class as well as the destination pointed to it.Since (1) and (2) refer to different instances of the RecDialog class, the value of mRecorder set in (1) will not be taken over by (2).(2)mRecorder remains uninitialized (null), and attempts to invoke the 出そうinstance methodッド stopRecording for null results in a nullPointerException exception.

This is not specific to the Android environment, but is based on the specifications of the programming language Java, so you might want to reconfirm the basics of Java around here (it should always be mentioned in a regular Java introduction).Keywords include ンスinstance variable 」 and インスタinstance method が).


2022-09-30 20:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.