Reputation: 32
Maybe someone sees an error, the problem is that when i push btn2 (button 2)
and btn3 (button 3)
app crashes
, but action still works, i.e video
is running and PDF
opens, while button 1
is working correctly...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
configureImageButton();
mCamera = getCameraInstance();
mPreview = new CameraPreview(this, mCamera);
FrameLayout layout = (FrameLayout) findViewById(R.id.camera_View);
layout.addView(mPreview);
controlInflater = LayoutInflater.from(getBaseContext());
View view = getLayoutInflater().inflate(R.layout.activity_main, layout, false);
layout.addView(view);
}
public Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
c.setDisplayOrientation(90);
}
catch (Exception e){
}
return c;
}
@Override
protected void onPause() {
super.onPause();
mpAudio.release();
}
private void configureImageButton() {
ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton);// audio button
ImageButton btn2 = (ImageButton) findViewById(R.id.imageButton2); // video button
ImageButton btn3= (ImageButton) findViewById(R.id.imageButton3);//reading button
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mpAudio = MediaPlayer.create(MainActivity.this,R.raw.tirepressuremonitoringsystem);
mpAudio.start();
}
}
);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Activity2.class));
}
}
);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readPDF();
}
}
);
}
private void readPDF()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "tirepressuremonitoringsystem3.pdf");
try
{
in = assetManager.open("tirepressuremonitoringsystem3.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/tirepressuremonitoringsystem3.pdf"),
"application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
public void playSound () throws IOException {
AssetFileDescriptor afd = getAssets().openFd("tirepressuremonitoringsystem.mp3");
mpAudio = new MediaPlayer();
mpAudio.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mpAudio.prepare();
mpAudio.start();
}
@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);
}
}
Upvotes: 0
Views: 56
Reputation: 413
It crashed if you dont press btn1 because without press it, mpAudio
will be null.
Then when onPause
call, mpAudio.release();
will cause NullPointerException.
Note that: onPause is called whenever the activity is not shown on screen but is still running(in your case, you start other activity with btn2,3 then it will be called).
Please correct to
@Override
protected void onPause() {
super.onPause();
if(mpAudio!=null)
mpAudio.release();
}
Have fun!
Upvotes: 1