Oussaki
Oussaki

Reputation: 1649

Can't startActivity from a fragment

and i want when i click in a TextView that another activity will start , but the VM show to me an exception i don't know why this is my code

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_main,
            container, false);

    // Ajouter un listener leur du click sur Nouveau utilisateur
    TextView TextInscription = (TextView) rootView
            .findViewById(R.id.textInscription);
    TextInscription.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent(rootView.getContext(),
                    Inscription.class);
            getActivity().startActivity(intent);

        }
    });

    return rootView;
}

}

Upvotes: 0

Views: 754

Answers (1)

Philio
Philio

Reputation: 4185

Without seeing your stack trace I can't say if this is the cause of the exception. I would use the activity as the context:

Intent intent = new Intent(getActivity(), Inscription.class);

Upvotes: 1

Related Questions