-3

I want to call a class using button created in the fragment. This is the code for the fragment which contains the button.

public class FragmentSecondPage extends Fragment{ View root; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){ root = inflater.inflate(R.layout.fragment_page2, container, false); return root; } } 

I want to start a class SplashActivity.class from the button present infragment_page2. Could somebody please help me?

3
  • first you can start using google: How to start new activity on button click. Then you can click on the first result which brings you to stackoverflow. stackoverflow.com/questions/4186021/… Commented Jan 26, 2014 at 18:37
  • How is this different from you previous question that I answered? Commented Jan 26, 2014 at 18:44
  • the problem was that i was getting an error msg at the line Button bt = (Button) findViewById(R.id.button1); i had to convert it to Button bt = (Button) root.findViewById(R.id.button1); could you please tell me the difference for future reference and i got it running your code itself thank. Commented Jan 27, 2014 at 0:57

1 Answer 1

2
//Create Intent that would call the Next Activity and since you are in Fragment you need to call getActivity() instead of this Intent myIntent = new Intent(getActivity(), SplashActivity.class); // This function will replace the currently running activity with new one getActivity().startActivity(myIntent); 
Sign up to request clarification or add additional context in comments.

2 Comments

Please comment the code as well...
Fragment is part of activity, thus its not activity findviewbyid is method of class Activity not Fragment thus you need to call the current activity using getActivity().whateverfunction()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.