Question

package com.example.projectlayout;

import java.io.File;

import android.media.MediaRecorder;

import android.os.Bundle;

import android.os.Environment;

import android.os.SystemClock;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.Chronometer;

import android.widget.Toast;


public class MainActivity extends Activity {

Chronometer time;
 int i=0;
MediaRecorder recorder;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    time=(Chronometer)findViewById(R.id.chronometer1);
    String name="myrecord"+i++;
    File sdcard=Environment.getExternalStorageDirectory();
    System.out.println(sdcard.toString());
    File recording=new File(sdcard,"videoRecordingFileZ");
    if(!recording.exists())
    {
    System.out.println("inside if");
    recording.mkdir();
    }
String rec=recording.getAbsolutePath()+"/Record"+".3GP";
    try{                
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(rec);                                    
    recorder.prepare();
    }
    catch(Exception e){
    System.out.println(e);
    recorder.start();
    }}
   public void startrecording(View v)
    {
        Toast.makeText(MainActivity.this, "START RECORDING", Toast.LENGTH_LONG).show();
        time.setBase(SystemClock.elapsedRealtime());
        time.start();
    }
public void stoprecording(View v)
    {
        Toast.makeText(MainActivity.this, "STOP RECORDING", Toast.LENGTH_LONG).show();
        time.stop();
    }
    public void showrecording(View v)
    {
        Toast.makeText(MainActivity.this, "Show RECORDING", Toast.LENGTH_LONG).show();
        Intent i=new Intent(MainActivity.this,Showlist.class);
        startActivity(i);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

Logcat:

03-27 00:49:22.224: E/AndroidRuntime(470): FATAL EXCEPTION: main

03-27 00:49:22.224: E/AndroidRuntime(470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectlayout/com.example.projectlayout.MainActivity}: java.lang.NullPointerException

03-27 00:49:22.224: E/AndroidRuntime(470): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectlayout/com.example.projectlayout.MainActivity}: java.lang.NullPointerException
Was it helpful?

Solution

You did not initialize your MediaRecorder:

 MediaRecorder recorder;

you should do it in the onCreate method:

 recorder = new MediaRecorder();

before you make use of your recorder or you get a NullPointerException.

BTW: improve your code indentation (in Eclipse Ctrl + I keybord shortcut) to increase readability of your code


about the next problem: IllegalStateException:

check the MediaRecorder state diagram from the android documentation: enter image description here

and the provided example code:

MediaRecorder recorder = new MediaRecorder();
 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 recorder.setOutputFile(PATH_NAME);
 recorder.prepare();
 recorder.start();   // Recording is now started
 ...
 recorder.stop();
 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
 recorder.release(); // Now the object cannot be reused

OTHER TIPS

You really need to provide the full error message and call stack if you want to get help for an exception.

Having said that, I can see a problem in your code (assuming you have provided all the relevant parts).

The 'recorder' member variable is never assigned a value, but is dereferenced in the try AND catch clauses of onCreate(). It is probably the dereference in the catch clause that is causing the NullPointerException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top