Reputation: 1532
I have a fragment
public class TwitterFragment extends Fragment implements OnClickListener {}
From this fragment I am invoking an intent as:
Intent intent = new Intent(this, WebViewActivity.class);
where,
public class WebViewActivity extends Activity {
But Invoking intent is giving error:
Error:(214, 33) error: no suitable constructor found for Intent(TwitterFragment,Class<WebViewActivity>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; TwitterFragment cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; TwitterFragment cannot be converted to Context)
This code was working when TwitterFragment class extends Activity instead of fragment.
Upvotes: 1
Views: 1175
Reputation: 961
Use getActivity() instead of this. Intent intent = new Intent(getActivity(), WebViewActivity.class);
Upvotes: 1
Reputation: 4292
Intent demands a Context
as its first parameter. Do this:
Intent intent = new Intent(getActivity(), YourTargetClass.class);
And to actually start your Activity
, do this:
getActivity().startActivity(intent);
Fragment
doesn't extend Context
, so it borrows it from its housing Activity
. That is why you can't get your Intent
working the first time you try it.
Upvotes: 2
Reputation: 2436
Get the activity for the context parameter
Intent intent = new Intent(getActivity(), WebViewActivity.class);
Upvotes: 1
Reputation: 4573
Fragment
is not a Context
. Try this:
Intent intent = new Intent(getActivity(), WebViewActivity.class);
Upvotes: 4