Android App Force在AlertDialog上按“保存”按钮后关闭



当用户按下按钮时,弹出窗口打开,要求输入名称,用此名称音频存储在SD卡中。

现在我的问题是当用户按"保存"按钮在警报对话框上发生错误时,正在发生java.lang.IllegalStateException and android app force closing

的错误

记录活动代码。

import android.app.Activity;
import android.content.DialogInterface;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class Record extends Activity {
String audioName= "";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private MediaRecorder recorder = null;
Chronometer myChronometer;
Handler seekHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);

    myChronometer = (Chronometer) findViewById(R.id.chronometer);
    Button recordingButton = (Button) findViewById(R.id.btnStart);

    recordingButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(Record.this,
                    "Start Recording With LongClick", Toast.LENGTH_SHORT)
                    .show();
            enableButtons(true);
            startRecording();
            return true;
        }
    });
    setButtonHandlers();
    enableButtons(false);
}
private void setButtonHandlers() {
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
}
private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart, !isRecording);
    enableButton(R.id.btnStop, isRecording);
}
private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);
    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + audioName +  ".wav");
}
private void startRecording() {
    displayAlertDialog();

}
private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
        myChronometer.stop();
    }
}

private void displayAlertDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            Record.this);
    // Setting Dialog Title
    alertDialog.setTitle("Would you Like to save your Recording");
    // Setting Dialog Message
    alertDialog.setMessage("Enter Audio Name");
    final EditText editTextAudioName = new EditText(Record.this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    editTextAudioName.setLayoutParams(lp);
    alertDialog.setView(editTextAudioName);

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("Save",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    audioName = editTextAudioName.getText().toString().trim();
                    recorder = new MediaRecorder();
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                    recorder.setAudioEncoder(MediaRecorder.OutputFormat.DEFAULT);
                    recorder.setOutputFile(getFilename());
                    recorder.setOnErrorListener(errorListener);
                    recorder.setOnInfoListener(infoListener);

                    try {
                        recorder.prepare();
                        recorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    myChronometer.setBase(SystemClock.elapsedRealtime());
                    myChronometer.start();
                }
            }).setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog
                    dialog.cancel();
                }
            });
    // Setting Negative "NO" Button
    alertDialog.show();
}

    private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record.this, "Error: " + what + ", " + extra,
                Toast.LENGTH_SHORT).show();
    }
};
private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Record.this,
                "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
                .show();
    }
};
private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStart: {
                Toast.makeText(Record.this, "Start Recording",
                        Toast.LENGTH_SHORT).show();
                enableButtons(true);
                startRecording();
                break;
            }
            case R.id.btnStop: {
                Toast.makeText(Record.this, "Stop Recording",
                        Toast.LENGTH_SHORT).show();
                enableButtons(false);
                stopRecording();
                // displayFormatDialog();
                break;
            }

        }
    }
};
}

和我的logcat

第138行= recorder.start();

FATAL EXCEPTION: main
Process: com.example.framesss.myproject, PID: 2254
                                                                          java.lang.IllegalStateException
                                                                              at android.media.MediaRecorder.start(Native Method)
                                                                              at com.example.framesss.myproject.Record$3.onClick(Record.java:138)
                                                                              at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

谢谢

您是否在mannifest中添加权限?

<uses-permission   android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" / >

最新更新