chronometer - Choronometer to save elapsed time and display in the ListView in android -
i have choronometer in app. working fine unable save recorded time. every time user clicks on stop button want capture time , save in database , display user in listview past recorded time. have have tried implement same theres error in code because app crashes when stopbutton clicked. pls can me out code have written follows:
studytimerecord:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { final view rootview = inflater.inflate(r.layout.fragment_studytimerecord, container, false); bundle bundle = getarguments(); mchaptername = bundle.getstring("chapter_name"); startbutton = (button) rootview.findviewbyid(r.id.start_button); stopbutton = (button) rootview.findviewbyid(r.id.stop_button); resetbutton = (button) rootview.findviewbyid(r.id.reset_button); studywatch = (chronometer) rootview.findviewbyid(r.id.chronometer); // method when press 'start' button startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // perform action on click studywatch.setbase(systemclock.elapsedrealtime() + timewhenstopped); studywatch.start(); stopclicked = false; return; } }); // method when press 'reset' button resetbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // perform action on click studywatch.setbase(systemclock.elapsedrealtime()); timewhenstopped = 0; // textview secondstext = (textview)getview().findviewbyid(r.id.hmstekst); // secondstext.settext("0 seconds"); } }); // method when press 'stop' button stopbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // perform action on click if (!stopclicked) { // textview secondstext = (textview)getview().findviewbyid(r.id.hmstekst); timewhenstopped = studywatch.getbase() - systemclock.elapsedrealtime(); int seconds = (int) timewhenstopped / 1000; // secondstext.settext( math.abs(seconds) + " seconds"); studywatch.stop(); stopclicked = true; string chronotext = studywatch.gettext().tostring(); string array[] = chronotext.split(":"); if (array.length == 2) { stoppedmilliseconds = integer.parseint(array[0]) * 60 * 1000 + integer.parseint(array[1]) * 1000; } else if (array.length == 3) { stoppedmilliseconds = integer.parseint(array[0]) * 60 * 60 * 1000 + integer.parseint(array[1]) * 60 * 1000 + integer.parseint(array[2]) * 1000; } final dialog dialog = new dialog(getactivity()); dialog.setcontentview(r.layout.alert_dialog); dialog.show(); dialog.findviewbyid(r.id.yesbutton).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { databasestudylogs dbstudylog = new databasestudylogs(getcontext(),null,null,1); studylog log = new studylog(); log.chaptername = mchaptername; log.datetime =stoppedmilliseconds; log.d("yesbutton", mchaptername); dbstudylog.addstudylog(log); dialog.dismiss(); intent intent = new intent(getactivity(),studytimelogsactivity.class); intent.putextra("chapter_name", mchaptername); intent.putextra("date_time",mdatetime); getactivity().startactivity(intent); getactivity().finish(); } }); dialog.findviewbyid(r.id.nobutton).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dialog.dismiss(); } }); } } }); settitle(); return rootview; }
studytimelogsactivity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_study_time_logs); mchaptername = getintent().getstringextra("chapter_name"); mstudytime=getintent().getlongextra("study_time",mstudytime); databasestudylogs dbhandler = new databasestudylogs(getapplicationcontext(), null, null, 1); cursor cursor = dbhandler.getstudylogcursor(mchaptername); cursor cursor1 = dbhandler.getstudylogcursortime(mstudytime); studylogchapter = (textview)findviewbyid(r.id.studylogchapter); studylogtime =(textview)findviewbyid(r.id.studylogtime); // todo set description here mloglistview = (listview)findviewbyid(r.id.loglist); if (cursor != null && cursor.getcount() > 0) mloglistview.setadapter(new logadapter(getapplicationcontext(),cursor,0)); // mloglistview.setadapter(new logadapter1(getapplicationcontext(),cursor,0)); } /** * class render logs */ class logadapter extends cursoradapter { private textview mlogdate; private textview mlogchapter; public logadapter(context context, cursor c, boolean autorequery) { super(context, c, autorequery); } public logadapter(context context, cursor c, int flags) { super(context, c, flags); } @override public view newview(context context, cursor cursor, viewgroup parent) { view view = layoutinflater.from(context).inflate(r.layout.layout_log_list_item, parent, false); return view; } @override public void bindview(view view, context context, cursor cursor) { mlogdate = (textview)view.findviewbyid(r.id.studylogtime); mlogdate.settext(cursor.getint(cursor.getcolumnindex(databasestudylogs.column_date_time))+""); mlogchapter =(textview)view.findviewbyid(r.id.studylogchapter); mlogchapter.settext(cursor.getstring(cursor.getcolumnindex(databasestudylogs.column_chapter_name))); } } class logadapter1 extends cursoradapter { private textview mlogdate; private textview mlogchapter; public logadapter1(context context, cursor c, boolean autorequery) { super(context, c, autorequery); } public logadapter1(context context, cursor c, int flags) { super(context, c, flags); } @override public view newview(context context, cursor cursor, viewgroup parent) { view view = layoutinflater.from(context).inflate(r.layout.layout_log_list_item2, parent, false); return view; } @override public void bindview(view view, context context, cursor cursor) { // mlogdate = (textview)view.findviewbyid(r.id.studylogdate); //mlogdate.settext(cursor.getint(cursor.getcolumnindex(databasestudylogs.column_date_time))+""); mlogchapter =(textview)view.findviewbyid(r.id.studylogchapter); mlogchapter.settext(cursor.getstring(cursor.getcolumnindex(databasestudylogs.column_chapter_name))); } }
error
java.lang.runtimeexception: unable start activity componentinfo{com.studypal.khadija.studypal/com.studypal..studypal.studytime.studytimelogsactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'long java.lang.long.longvalue()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2358) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2420) @ android.app.activitythread.access$900(activitythread.java:154) @ android.app.activitythread$h.handlemessage(activitythread.java:1321) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5292) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:904) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:699) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'long java.lang.long.longvalue()' on null object reference @ com.studypal.khadija.studypal.studytime.studytimelogsactivity.oncreate(studytimelogsactivity.java:30) @ android.app.activity.performcreate(activity.java:5990) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) @ android.app.activitythread.performlaunchactivity(activitythread.java:2311) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2420) @ android.app.activitythread.access$900(activitythread.java:154) @ android.app.activitythread$h.handlemessage(activitythread.java:1321) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5292) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:904) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:699)
Comments
Post a Comment