代码中的运行时异常

时间:2015-09-04 10:35:14

标签: java android

在尝试运行此代码时发现运行时异常, 尝试过很多东西,但每次都失败了。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/javacodegeeksRecording.3gpp";
    myRecorder = new MediaRecorder();
    myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myRecorder.setOutputFile(outputFile);

    text = (TextView) findViewById(R.id.text1);
    startBtn = (Button)findViewById(R.id.start);
    stopBtn = (Button)findViewById(R.id.stop);
    playBtn = (Button)findViewById(R.id.play);
    stopPlayBtn  = (Button)findViewById(R.id.stopPlay);
}

public void a(View aa) {
    start(aa);
}

public void b(View bb) {
    stop(bb);
}

public void c(View cc) {
    play(cc);
}

public void d(View dd) {
    stopPlay(dd);
}

public void start(View view) {
    try {
        myRecorder.prepare();
        myRecorder.start();
    }
    catch (IllegalStateException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    text.setText("Recording Point: Recording");
    startBtn.setEnabled(false);
    stopBtn.setEnabled(true);
    Toast.makeText(getApplicationContext(), "Start recording...",Toast.LENGTH_SHORT).show();
}

public void stop(View view) {
    try {
        myRecorder.stop();
        myRecorder.release();
        myRecorder  = null;
        stopBtn.setEnabled(false);
        playBtn.setEnabled(true);
        text.setText("Recording Point: Stop recording");
        Toast.makeText(getApplicationContext(), "Stop recording...",Toast.LENGTH_SHORT).show();
    }
    catch (IllegalStateException e) {
        e.printStackTrace();
    }
    catch (RuntimeException e) {
        e.printStackTrace();
    }
}

public void play(View view) {
    try {
        myPlayer = new MediaPlayer();
        myPlayer.setDataSource(outputFile);
        myPlayer.prepare();
        myPlayer.start();
        playBtn.setEnabled(false);
        stopPlayBtn.setEnabled(true);
        text.setText("Recording Point: Playing");
        Toast.makeText(getApplicationContext(), "Start play the recording...",Toast.LENGTH_SHORT).show();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

public void stopPlay(View view) {
    try {
        if (myPlayer != null) {
            myPlayer.stop();
            myPlayer.release();
            myPlayer = null;
            playBtn.setEnabled(true);
            stopPlayBtn.setEnabled(false);
            text.setText("Recording Point: Stop playing");
            Toast.makeText(getApplicationContext(), "Stop playing the recording...",Toast.LENGTH_SHORT).show();
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:0)

这段代码对我没有任何意义。当你不使用它们时,为什么要将视图传递给你的功能?

public void start(View view) {... //view not used }
是的,如果没有堆栈跟踪,我们就无法帮助你。

我唯一的假设是你需要在你使用的每个函数中对你的mPlayer进行空值检查,就像你在stopPlay()中那样做

if (myPlayer != null) {
    mPlayer.doStuff(); 
}