java - NullPointerException Error in android -
i working on media recorder application using android api 17. however, fatal exception:main
, null pointer exception
error during run time: i.e, while pressing record button unfortunately stopped working
error. doing wrong?
here 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 .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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
this log cat error
- 03-11 05:13:04.803: e/androidruntime(814): fatal exception: main
- 03-11 05:13:04.803: e/androidruntime(814): java.lang.nullpointerexception
- 03-11 05:13:04.803: e/androidruntime(814): @ co.example.projectplay.mainactivity$4.onclick(mainactivity.java:106)
- 03-11 05:13:04.803: e/androidruntime(814): @ android.view.view.performclick(view.java:4204)
- 03-11 05:13:04.803: e/androidruntime(814): @ android.view.view$performclick.run(view.java:17355)
- 03-11 05:13:04.803: e/androidruntime(814): @ android.os.handler.handlecallback(handler.java:725)
- 03-11 05:13:04.803: e/androidruntime(814): @ android.os.handler.dispatchmessage(handler.java:92)
- 03-11 05:13:04.803: e/androidruntime(814): @ android.os.looper.loop(looper.java:137)
- 03-11 05:13:04.803: e/androidruntime(814): @ android.app.activitythread.main(activitythread.java:5041)
- 03-11 05:13:04.803: e/androidruntime(814): @ java.lang.reflect.method.invokenative(native method)
- 03-11 05:13:04.803: e/androidruntime(814): @ java.lang.reflect.method.invoke(method.java:511)
- 03-11 05:13:04.803: e/androidruntime(814): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793)
- 03-11 05:13:04.803: e/androidruntime(814): @ com.androi03-11 05:13:04.803: e/androidruntime(814): @ dalvik.system.nativestart.main(native method)d.internal.os.zygoteinit.main(zygoteinit.java:560)
- 03-11 05:13:04.803: e/androidruntime(814): @ dalvik.system.nativestart.main(native method)
- 03-11 05:13:08.062: i/process(814): sending signal. pid: 814 sig: 9
your code doesn't show assignment txtbutton
. should this:
txtbutton=(textview)findviewbyid(r.id.txtbutton);
before use first time.
Comments
Post a Comment