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
答案 0 :(得分:1)
如果您想获得异常帮助,您确实需要提供完整的错误消息和调用堆栈。
话虽如此,我可以在您的代码中看到问题(假设您提供了所有相关部分)。
'recorder'成员变量永远不会赋值,但在onCreate()的try AND catch子句中被取消引用。可能是catch子句中的dereference引起了NullPointerException。
答案 1 :(得分:1)
您没有初始化MediaRecorder:
MediaRecorder recorder;
您应该在onCreate方法中执行此操作:
recorder = new MediaRecorder();
在使用recorder
之前或者你得到NullPointerException。
BTW:改进代码缩进(在Eclipse Ctrl + I keybord快捷方式)以提高代码的可读性
关于下一个问题:IllegalStateException
:
从android文档中检查MediaRecorder state diagram:
和提供的示例代码:
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