android - I want my button to call a method in a fragment -


so have (workoutlist activity) -> (workoutlist fragment) -> (exerciselist fragment) -> (addexercise fragment)

theres button in addexercise, , want invoke method in exerciselist

the problem current set-up i'm getting `attempt invoke virtual method

void android.widget.button.setonclicklistener(android.view.view$onclicklistener)' on null object reference 

xml addexercise button:

<button     android:id="@+id/add_exercise_button"     android:layout_height="wrap_content"     android:layout_width="wrap_content"     android:text="@string/add_workout_message_done"     android:layout_weight="0"     android:onclick="addexercisedone"     /> 

code snippet of exerciselist inside oncreateview method:

     view rootview = inflater.inflate(r.layout.fragment_exercise_list, container, false);     button addexercisedonebutton = (button) getactivity().findviewbyid(r.id.add_exercise_button);     addexercisedonebutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             addexercisedone(v);         }     }); 

note: in response chinmay's suggestion** have viewroot set exerciselist's xml file can make adapter display listview, i'm not sure how return view created can reference buttons. example, made

final view tmpview = inflater.inflate(r.layout.fragment_add_exercise, container, false);      button addexercisedonebutton = (button) tmpview.findviewbyid(r.id.add_exercise_button); 

you getting nullpointerexception because reference addexercisedonebutton not valid one. haven't specified layout has button id - r.id.add_exercise_button , when trying use button give nullpointer

instead oncreateview should like

public static class examplefragment extends fragment {     @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         return inflater.inflate(r.layout.example_fragment, container, false);     } } 

make view refer view oncreateview returning button id

for reference : http://developer.android.com/guide/components/fragments.html

you doing

view rootview = inflater.inflate(r.layout.fragment_exercise_list, container, false);     button addexercisedonebutton = (button) getactivity().findviewbyid(r.id.add_exercise_button); 

instead of

     view rootview = inflater.inflate(r.layout.fragment_exercise_list, container, false);         button addexercisedonebutton = (button)  rootview.findviewbyid(r.id.add_exercise_button); 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -