简单的录音和播放应用程序崩溃

时间:2015-03-01 18:14:12

标签: java android exception stack-trace

我的应用程序的一部分有以下代码:

package com.example.ankur.easyvoicemail;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.Button;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;


public class MainActivity extends ActionBarActivity {


    private static final String LOG_TAG = "EasyVoicemail";
    private static String mFileName = null;


    private MediaRecorder mRecorder = null;
    private MediaPlayer   mPlayer = null;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

   Button button1 = (Button) findViewById(R.id.button1);
   Button button2 = (Button) findViewById(R.id.button2);
   public void addListenerOnButton() {
   button1.setOnClickListener(new OnClickListener() {
    boolean mStartRecording = true;
    @Override
        public void onClick(View view) {
            onRecord(mStartRecording);
            if (mStartRecording) {
                button1.setText("Stop recording");
            } else {
                button1.setText("Start recording");
            }
           mStartRecording = !mStartRecording;
        }
    });
   button2.setOnClickListener(new OnClickListener(){
      boolean mStartPlaying = true;
      @Override
        public void onClick (View view){
          onPlay(mStartPlaying);
          if (mStartPlaying) {
              button2.setText("Stop playing");
          } else {
              button2.setText("Start playing");
          }
          mStartPlaying = !mStartPlaying;
      }
   });

   }

    public MainActivity() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/easy_voicemail.3gp";
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

}

该应用程序运行正常,但经过几次更改后,它开始崩溃。现在我不记得应用程序开始崩溃后的确切变化。 无法弄清楚我做错了什么。有什么帮助吗?

我的堆栈跟踪说:

03-01 14:03:01.796: E/AndroidRuntime(1424): Caused by: java.lang.NullPointerException
03-01 14:03:01.796: E/AndroidRuntime(1424):     at android.app.Activity.findViewById(Activity.java:1884)
03-01 14:03:01.796: E/AndroidRuntime(1424):     at com.example.ankur.easyvoicemail.MainActivity.<init>(MainActivity.java:102)

我不明白。这两个ID已经存在。 activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ankur.easyvoicemail.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/welcome_msg" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"
        android:text="@string/record" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_centerVertical="true"
        android:text="@string/play" />

    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你的Android清单好吗?!如果不是那么这是一个错误! <uses-permission android:name="RECORD_AUDIO"/>