Your code doesn't show an assignment to txtButton
. It probably should look like this:
txtButton=(TextView)findViewById(R.id.txtButton);
before you use it for the first time.
Frage
I am working on a Media Recorder application using android API 17. However, I get Fatal exception:main
and Null Pointer Exception
error during run time: i.e, while pressing record button get unfortunately stopped working
error. What am I doing wrong?
Here is my mainfest file:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="co.example.projectplay.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
This is my .java file
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FILE=Environment.getExternalStorageState()+"/tempRecord.3gpp";
botn1=(Button)findViewById(R.id.botn1);
botn1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
play= MediaPlayer.create(MainActivity.this,R.raw.a);
play.start();
play.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer play)
{
play.release();
}
}
);
}
});
botn2=(Button)findViewById(R.id.botn2);
botn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
play= MediaPlayer.create(MainActivity.this,R.raw.b);
play.start();
play.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer play)
{
play.release();
}
});
}
});
botn3=(Button)findViewById(R.id.botn3);
botn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
play= MediaPlayer.create(MainActivity.this,R.raw.c);
play.start();
play.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer play)
{
play.release();
}
});
}
});
botnRecord=(Button)findViewById(R.id.botnRecord);
botnRecord.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (botnRecord.getText().toString().equals("Record"))
{
try
{
startRecord();
}
catch (Exception e)
{
e.printStackTrace();
}
txtButton.setText("Recording...");
botnRecord.setText("End");
}
else if(botnRecord.getText().toString().equals("End"))
{
stopRecord();
txtButton.setText("");
botnRecord.setText("Play");
}
else if(botnRecord.getText().toString().equals("Play"))
{
try {
startPlayback();
}
catch (Exception e)
{
e.printStackTrace();
}
botnRecord.setText("Stop");
}
else
{
stopPlayback();
botnRecord.setText("Record");
}
}
});
}
public void startRecord()throws Exception
{
if(record!=null)
{
//record.release();
record.reset();
}
File fileOut=new File(FILE);
if(fileOut!=null)
{
fileOut.delete();
}
record=new MediaRecorder();
record.setAudioSource(MediaRecorder.AudioSource.MIC);
record.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
record.setOutputFile(FILE);
record.prepare();
record.start();
}
public void stopRecord()
{
record.stop();
record.release();
}
public void startPlayback()throws Exception
{
if(play!=null)
{
play.stop();
play.release();
}
play=new MediaPlayer();
play.setDataSource(FILE);
play.prepare();
play.start();
play.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer play)
{
play.release();
}
}
);
}
public void stopPlayback()
{
play.stop();
play.release();
}
@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;
}
}
This is my log cat error
Lösung
Your code doesn't show an assignment to txtButton
. It probably should look like this:
txtButton=(TextView)findViewById(R.id.txtButton);
before you use it for the first time.