Passing data between Fragments in Android -
i know many have answered question, still cannot mine work. in startworkout class data chronometer, , since data in milliseconds use method coded called showelapsedtime(). convert milliseconds seconds,minutes, , hours. want pass data class called workouts, reason not working.i not getting data because nullpointerexecption
here code.
startworkout class
package com.example.d_jara.apprunners; public class startworkout extends fragment{ button button; button button2; chronometer mchronometer; private int hours = 0; private int minutes = 0; private int seconds=0; @override public view oncreateview( layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.lay_startworkout, container, false); mchronometer = (chronometer) rootview.findviewbyid(r.id.chronometer); // mchronometer.setformat("h:mm:ss"); button = (button) rootview.findviewbyid(r.id.mystart); button.setonclicklistener(mstartlistener); mchronometer.settextsize(70); button2 = (button) rootview.findviewbyid(r.id.finishwork); //button2.setonclicklistener(mfinishlistener); //finish workout button rootview.findviewbyid(r.id.finishwork).setvisibility(rootview.gone); //test code here button2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { mchronometer.stop(); button.setvisibility(v.visible); button2.setvisibility(v.gone); toast toast = toast.maketext(getactivity().getbasecontext(),"your workout saved", toast.length_long); toast.show(); toast.setgravity(gravity.center|gravity.center, 0, 0); showelapsedtime(); int c= 9; bundle args = new bundle(); fragment newfragment = new workouts(); //number of calories args.putint("hours",hours); args.putint("seconds",seconds); args.putint("hours",hours); newfragment.setarguments(args); } }); return rootview; } view.onclicklistener mstartlistener = new view.onclicklistener() { public void onclick(view v) { mchronometer.setbase(systemclock.elapsedrealtime()-0); mchronometer.start(); button.setvisibility(v.gone); button2.setvisibility(v.visible); } }; private void showelapsedtime() { long elapsedmillis = systemclock.elapsedrealtime() - mchronometer.getbase(); hours = (int) (elapsedmillis / 3600000); minutes = (int) (elapsedmillis - hours * 3600000) / 60000; seconds = (int) (elapsedmillis - hours * 3600000 - minutes * 60000) / 1000; } }
and here workouts class
public class workouts extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.lay_workouts, container, false); int myseconds = getarguments().getint("seconds"); int myhours = getarguments().getint("hours"); int myminutes = getarguments().getint("minutes"); string se=string.valueof(myseconds); string minu=string.valueof(myminutes); string hours=string.valueof(myhours); textview sec1 = (textview) rootview.findviewbyid(r.id.seconds); textview minute1 = (textview) rootview.findviewbyid(r.id.minutes); textview hours1 = (textview) rootview.findviewbyid(r.id.minutes); sec1.settext(se); minute1.settext(minu); hours1.settext(hours); return rootview; } }
bundle args = new bundle(); fragment newfragment = new workouts(); //number of calories args.putint("hours",hours); args.putint("seconds",seconds); args.putint("hours",hours); newfragment.setarguments(args); fragmenttransaction ft = getfragmentmanager().begintransaction(); ft.add(yourcontainerid, newfragment, "workout"); ft.commit();
Comments
Post a Comment