java - Application auto shuts after getting spinner item and calculating -
whenever press button calculate, app auto shuts. due spinners(i have separate app test other fields without spinner), method of getting item spinner , using in condition correct? appreciated!
mainactivity
public class mainactivity extends activity implements onitemselectedlistener{ edittext height; edittext weight; edittext age; textview calories; button calculate; double h,w,wc,hc,m,wm,z,a,ac; spinner spinner, spinner2; string choice; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initcontrols(); // spinner element spinner spinner = (spinner) findviewbyid(r.id.spinner); // spinner click listener spinner.setonitemselectedlistener(this); // spinner drop down elements list<string> activity = new arraylist<string>(); activity.add("sedentary(little/no)"); activity.add("lightly active(1-3days/week)"); activity.add("moderately(3-5days/week)"); activity.add("very active(6-7days/week)"); activity.add("extremely active(physical job/2x training)"); // spinner element spinner spinner2 = (spinner) findviewbyid(r.id.spinner2); // spinner click listener spinner2.setonitemselectedlistener(this); // spinner drop down elements list<string> gender = new arraylist<string>(); gender.add("male"); gender.add("female"); // creating adapter spinner arrayadapter<string> activityadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, activity); arrayadapter<string> genderadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, gender); // drop down layout style - list view radio button activityadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // attaching data adapter spinner spinner.setadapter(activityadapter); spinner2.setadapter(genderadapter);} @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { // on selecting spinner item string item = parent.getitematposition(position).tostring(); // showing selected spinner item toast.maketext(parent.getcontext(), "selected: " + item, toast.length_long).show(); } public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } private void initcontrols(){ height=(edittext)findviewbyid(r.id.height); weight=(edittext)findviewbyid(r.id.weight); age=(edittext)findviewbyid(r.id.age); calories=(textview)findviewbyid(r.id.calories); calculate=(button)findviewbyid(r.id.calculate); calculate.setonclicklistener(new button.onclicklistener() {public void onclick (view v) { calculate();}}); } private void calculate(){ h=double.parsedouble(height.gettext().tostring()); w=double.parsedouble(weight.gettext().tostring()); a=double.parsedouble(age.gettext().tostring()); hc=h*6.25; wc=w*10; ac=a*5; m= wc+hc-ac+5; wm=wc+hc-ac-161; log.e("error","messagehere1"); string choice = spinner.getselecteditem().tostring(); switch(choice){ case "sedentary(little/no)": if(spinner2.getselecteditem().equals("male")){ z=m*1.2; calories.settext(double.tostring(z)); } else{ z=wm*1.2; calories.settext(double.tostring(z));} case "lightly active(1-3days/week)": if(spinner2.getselecteditem().equals("male")){ z=m*1.375; calories.settext(double.tostring(z)); } else{ z=wm*1.375; calories.settext(double.tostring(z));} case "moderately(3-5days/week)": if(spinner2.getselecteditem().equals("male")){ z=m*1.55; calories.settext(double.tostring(z)); } else{ z=wm*1.55; calories.settext(double.tostring(z));} case "very active(6-7days/week)": if(spinner2.getselecteditem().equals("male")){ z=m*1.725; calories.settext(double.tostring(z)); } else{ z=wm*1.725; calories.settext(double.tostring(z));} case "extremely active(physical job/2x training)": if(spinner2.getselecteditem().equals("male")){ z=m*1.9; calories.settext(double.tostring(z)); } else{ z=wm*1.9; calories.settext(double.tostring(z));} } }
logcat
05-12 10:17:37.339: i/choreographer(6028): skipped 50 frames! application may doing work on main thread. 05-12 10:17:37.379: i/pga(6028): attempting create new socket connectionn pid = 6028, tid = 6028 05-12 10:17:37.389: i/pga(6028): new socket connection: com.example.calories (pid 6028, tid 6028) 05-12 10:19:38.859: e/error(6276): messagehere1 05-12 10:19:38.859: d/androidruntime(6276): shutting down vm 05-12 10:19:38.859: w/dalvikvm(6276): threadid=1: thread exiting uncaught exception (group=0x55e15b20) 05-12 10:19:38.859: i/process(6276): sending signal. pid: 6276 sig: 9 05-12 10:19:38.859: d/androidruntime(6276): procname cmdline: com.example.calories 05-12 10:19:38.859: e/androidruntime(6276): in writecrashedappname, pkgname :com.example.calories 05-12 10:19:38.859: d/androidruntime(6276): file written content: com.example.calories stringbuffer : ;com.example.calories
change these lines
// spinner element spinner spinner = (spinner) findviewbyid(r.id.spinner); // spinner element spinner spinner2 = (spinner) findviewbyid(r.id.spinner2);
with
spinner = (spinner) findviewbyid(r.id.spinner); spinner2 = (spinner) findviewbyid(r.id.spinner2);
this work.
Comments
Post a Comment