从片段调用intent

时间:2015-07-15 01:14:31

标签: java android android-fragments android-intent

我有一个片段

public class TwitterFragment extends Fragment implements OnClickListener {}

从这个片段我调用一个意图:

Intent intent = new Intent(this, WebViewActivity.class);

其中,

public class WebViewActivity extends Activity {

但是调用意图是错误的:

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)

当TwitterFragment类扩展Activity而不是片段时,此代码正常工作。

2 个答案:

答案 0 :(得分:4)

Fragment不是Context。试试这个:

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

答案 1 :(得分:1)

使用getActivity()代替此。 Intent intent = new Intent(getActivity(),WebViewActivity.class);