从片段中调用意图 [英] Invoking intent from a fragment

查看:50
本文介绍了从片段中调用意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段

public class TwitterFragment extends Fragment implements OnClickListener {}

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

From this fragment I am invoking an intent as:

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

哪里

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)

当TwitterFragment类扩展Activity而不是fragment时,此代码有效.

This code was working when TwitterFragment class extends Activity instead of fragment.

推荐答案

Fragment 不是 Context .试试这个:

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

这篇关于从片段中调用意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆