Home > Mobile >  Add a click event within the [for] fragments after not to jump to the target Activity
Add a click event within the [for] fragments after not to jump to the target Activity

Time:10-06

This is my code, the onActivityCreated () to add the activity, but no response after click the button, the Android small white,,,

 public class Fragment2 extends fragments {
Private Button BTN.
@ Nullable
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


View the View=inflater. Inflate (R.l ayout. Fragment2, container, false);
Return the view;

}

@ Override
Public void onActivityCreated (@ Nullable Bundle savedInstanceState) {
Super. OnActivityCreated (savedInstanceState);
BTN=(Button) getActivity (). The findViewById (R.i which est_bt);
BTN. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
Toast. MakeText (getContext (), "success2", Toast. LENGTH_SHORT), show ();

StartActivity (new Intent (getActivity (), PayActivity. Class));
}
});

}
}

CodePudding user response:

Fragments need getActivity (). StartActivity (new Intent (getActivity (), PayActivity. Class));

CodePudding user response:

Why the activity of the button to set the click event at the fragments? If it is a fragment of the button, you should set the click event in onViewCreated method

CodePudding user response:

reference 1/f, the wind light cloud light v10 reply:
fragments inside need to getActivity () startActivity (new Intent (getActivity (), PayActivity. Class));


In addition you need in onViewCreated method using the findViewById to obtain control
  • Related