如何从片段TextView打开URL? [英] How to open URL from Fragment TextView?

查看:99
本文介绍了如何从片段TextView打开URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个选项卡的活动,每个选项卡都包含一个片段,其中包含一些文本.我想要一个带有阅读更多"的文本,它是指向URL的链接.没有链接,一切正常,但是当我尝试实现它时,我得到

I have an activity with multiple tabs and each tab consists of a fragment with some text inside. I would like to have a text with "Read More" which is a link to an URL. Without having the link, everything works fine, but when I try to implement it, I get

E/UncaughtException:java.lang.NullPointerException

E/UncaughtException: java.lang.NullPointerException

所以我认为这是实现它的方式.现在,该片段具有以下内容:

So I assume it is the way I implement it. Right now, the fragment has this:

public class About_us extends Fragment {

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_about_us, container, false);
    //The part below is For Read More
    TextView t2 = (TextView) getView().findViewById(R.id.read_more);
    if (t2 != null) {
      t2.setMovementMethod(LinkMovementMethod.getInstance());
    }

    return rootView;
  }
}

"read_more"布局的TextView具有以下功能:

"read_more" layout has this for the TextView:

< TextView
android: id = "@+id/read_more"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: clickable = "true"
android: text = "@string/link_to_the_website"
android: textColor = "@color/buttonColorPressed" / >

在字符串中给出了link_to_website:

And link_to_website is given in the strings:

< string name = "link_to_the_website" > < a href = "www.google.com/" > Read More Here < /a></string >

有人可以帮我弄清楚我写错了什么吗?

Can anyone help me to figure out what is it that I wrote wrong?

推荐答案

将膨胀视图用作

TextView t2 = (TextView) rootView.findViewById(R.id.read_more); 

或覆盖onViewCreated,然后您可以使用getView()或定向传递的视图

or override onViewCreated and then you can use getView() or directed passed view

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    TextView t2 = (TextView) view.findViewById(R.id.read_more);
    // or TextView t2 = (TextView) getView().findViewById(R.id.read_more);
}

因为getview仅返回onCreateView 先前创建并返回的实际视图,否则将返回null ,因此出现问题

Because getview will only return the actual view which was previously created and returned by onCreateView otherwise it will return null, hence the issue

Get the root view表示片段的布局(one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)),如果提供的话.

Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.

,因此您不能在onCreateView完成之前使用getView. 然后使用这种方法,将任务分为两部分

so you cannot use getView before the completion of onCreateView. Then with this approach, you split the task into two parts

onCreateView:查看通货膨胀

onViewCreated:用于视图和侦听器初始化

onViewCreated: for view and listeners initialization

更新:添加链接

t2.setMovementMethod(LinkMovementMethod.getInstance());
t2.setText(Html.fromHtml("< string name = 'link_to_the_website' > < a href = 'https://www.google.com/' > Read More Here < /a></string >"));

这篇关于如何从片段TextView打开URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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