Android simple voice capture button, NullPointerException

I'm trying to make my self a simple rec button for capture audio from user. I find the guide here and i'm trying to change something but an error occurs and i don't know what is the problem.

Logcat error:

12-03 18:43:21.773  11093-11093/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ddz.diarioscolastico/com.ddz.diarioscolastico.ActivityRegistrazione}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.ddz.diarioscolastico.ActivityRegistrazione.onCreate(ActivityRegistrazione.java:114)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at 

And there is my changed code

public class ActivityRegistrazione extends Activity {

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

    //private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;

    //private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;

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

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        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;
    }

    /*class RecordButton extends Button {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }*/

    public ActivityRegistrazione() {
        //mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        File mFolder = new File(Environment.getExternalStorageDirectory() + "/diarioscolastico");
        mFileName = Environment.getExternalStorageDirectory() + "/diarioscolastico";


        boolean success = true;
        if (!mFolder.exists()) {
            success = mFolder.mkdir();
        }
        if (success) {

            // Create an audio file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String audioFileName = "AUDIO_" + timeStamp + "_.3gp";
            // Do something on success
            mFileName += "/"+ audioFileName ;

        } else {
            // Do something else on failure
        }

    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        final Button recButton = (Button) findViewById(R.id.recButton);
        recButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                boolean mStartRecording = true;

                onRecord(mStartRecording);
                if (mStartRecording) {
                    recButton.setText("Stop recording");
                } else {
                    recButton.setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        });
    }

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

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_activity_registrazione, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

The problem is that the example use a button created from java, but i need a static button from xml, so i try to generate it and search it with the id but when i launch the activity the app crash and the error is in the logcat.

Some ideas??

Answers


I think that you miss a line on onCreate handler.

After calling super.onCreate(...) and before search for the views, you should set the content, something like this:

super.onCreate(icicle);
setContentView(R.layout.mylayout);
final Button recButton = (Button) findViewById(R.id.recButton);

where mylayout is the layout name associated with this view and containing the recButton.


Need Your Help

I've deleted my keystore. Now what?

java certificate keystore keytool csr

I've generated a csr file and send it to a CA to sign and generate a certificate.

How to erase an object from window in case of multi object/structure X11 display?

c unix graphics x11 displayobject

In case of X11 display programming, I was wondering how to erase a structure from window once it is displayed. Let's say I want to draw a straight line, rectangle, and circle. However, I want the