android - App crashing after pushing button, but the action is working -


maybe sees error, problem when push btn2 (button 2) , btn3 (button 3) app crashes, action still works, i.e video running , pdf opens, while button 1 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. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }    } 

it crashed if dont press btn1 because without press it, mpaudio null. when onpause call, mpaudio.release(); cause nullpointerexception.

note that: onpause called whenever activity not shown on screen still running(in case, start other activity btn2,3 called).

please correct to

@override protected void onpause() {     super.onpause();     if(mpaudio!=null)         mpaudio.release(); } 

have fun!


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -