listview - Android: findViewById on Fragment Item returns null -


i cant figure out.

when clicks on item in listview, want open fragment , set 1 edittext , 3 radiobuttons according values of list item clicked.

public void openedittask(int position) {     if (newtaskfragment != null) closenewtaskfragment();     if (edittaskfragment != null) closeedittaskfragment();      edittaskfragment = new edittaskfragment();     transaction = getsupportfragmentmanager().begintransaction();     transaction.setcustomanimations(r.anim.enter_from_right, r.anim.exit_to_right);     transaction.replace(r.id.edit_task_frame_xml, edittaskfragment);      //set edittext , radiobuttons according clicked item     edittext edittaskname = (edittext) findviewbyid(r.id.edit_task_name_xml);     radiobutton easybutton = (radiobutton) findviewbyid(r.id.edit_radio_button_easy_xml);     radiobutton mediumbutton = (radiobutton) findviewbyid(r.id.edit_radio_button_medium_xml);     radiobutton hardbutton = (radiobutton) findviewbyid(r.id.edit_radio_button_hard_xml);     //edit text     edittaskname.settext(mtasklist.get(position).getname());     //radio buttons     if (mtasklist.get(position).getdifficultynumber() == 1) {         easybutton.ischecked();     } else if (mtasklist.get(position).getdifficultynumber() == 2) {         mediumbutton.ischecked();     } else hardbutton.ischecked();      transaction.commit();  } 

why null pointer exception? dont understand it.

you should handle instances in fragment class. can set position argument , data wherever need , show view.

your edittaskfragment should this:

public class edittaskfragment extends fragment {      private static final string position_param = "positionparam";      private int position;      public edittaskfragment() {     }      public static edittaskfragment newinstance(int position) {         edittaskfragment fragment = new edittaskfragment();         bundle args = new bundle();         args.putint(position_param, position);         fragment.setarguments(args);         return fragment;     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         if (getarguments() != null) {             position = getarguments().getint(position_param);         }     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view v = inflater.inflate(r.layout.yourfragmentlayout, container, false);         edittext edittaskname = (edittext) v.findviewbyid(r.id.edit_task_name_xml);         radiobutton easybutton = (radiobutton) v.findviewbyid(r.id.edit_radio_button_easy_xml);         radiobutton mediumbutton = (radiobutton) v.findviewbyid(r.id.edit_radio_button_medium_xml);         radiobutton hardbutton = (radiobutton) v.findviewbyid(r.id.edit_radio_button_hard_xml);         //edit text         edittaskname.settext(mtasklist.get(position).getname());         //radio buttons         if (mtasklist.get(position).getdifficultynumber() == 1) {             easybutton.ischecked();         } else if (mtasklist.get(position).getdifficultynumber() == 2) {             mediumbutton.ischecked();         } else             hardbutton.ischecked();          return v;       }     } 

then, openedittask function should be:

public void openedittask(int position) {     if (newtaskfragment != null) closenewtaskfragment();     if (edittaskfragment != null) closeedittaskfragment();      edittaskfragment = edittaskfragment.newinstance(position);     transaction = getsupportfragmentmanager().begintransaction();     transaction.setcustomanimations(r.anim.enter_from_right, r.anim.exit_to_right);     transaction.replace(r.id.edit_task_frame_xml, edittaskfragment);         transaction.commit();  } 

so, have data according position user selected, in fragment class.

i hope answer question


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -