【问题标题】:How to open URL from Fragment TextView?如何从 Fragment TextView 打开 URL?
【发布时间】:2018-06-24 03:46:16
【问题描述】:

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

E/UncaughtException: java.lang.NullPointerException

所以我认为这是我实现它的方式。现在,片段有这个:

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 有这个:

< 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:

&lt; string name = "link_to_the_website" &gt; &lt; a href = "www.google.com/" &gt; Read More Here &lt; /a&gt;&lt;/string &gt;

谁能帮我弄清楚我写错了什么?

【问题讨论】:

    标签: android android-fragments url hyperlink


    【解决方案1】:

    以太使用你的膨胀视图

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

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

    @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,因此会出现问题

    getView()

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

    所以你不能在onCreateView 完成之前使用getView。 然后通过这种方法,您将任务分成两部分

    onCreateView:用于视图膨胀

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

    更新:添加链接

    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 t2 = (TextView) rootView.findViewById(R.id.read_more);什么也没有发生。请看上面的评论。如果这样解决不了,我就换个方式试试
    • @CuriousPaul 不要尝试其他方式,不会改变任何东西,我会更新链接部分的答案
    • 感谢您与我一起解决这个问题。好消息是该链接有效并打开了浏览器,这很棒。问题是,它显示整行“ Read More Here ”),而不是 Read More Here google.com 突出显示。我还有什么需要做的吗?
    • @CuriousPaul 尝试删除 &lt; string ...&gt; &lt;/string&gt; 对,使用 &lt; a href = 'https://www.google.com' &gt; Read More Here &lt; /a&gt;
    • 做到了。现在它应该显示带下划线的“在这里阅读更多”,但它没有做任何事情。这就是我现在所拥有的:t2.setText(Html.fromHtml("&lt;a href = 'www.google.com/' &gt; Read More Here &lt;/a&gt;")); 我也导入了 import android.text.Html;
    【解决方案2】:
    // declare interface in fragment   
     public interface OnTextSelectedListener
      {
       void ontextSelected(String url);
      }
    
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
    
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnTextSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnTextSelectedListener");
        }
    }
    
    // Implement interface in activity
    
    
       public void ontextSelected(String   url) {
      // load your webview
       }
    

    查看此link 以更好地了解片段通信。

    【讨论】:

    • 谢谢!这会在 onCreateView 之后进行吗?我应该保持布局和字符串代码相同吗?
    • onCreateView 调用侦听器方法并更好地理解读取片段通信。如果对您有帮助,请接受我的回答。@CuriousPaul
    【解决方案3】:

    尝试更改代码中的行

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

    并在 textview 中的 xml 中添加属性

    android:autoLink="web" 
    

    【讨论】:

    • 谢谢!这部分:TextView t2 = (TextView) rootView.findViewById(R.id.read_more);解决了崩溃问题,但添加 autoLink 实际上会杀死链接。但是,如果我删除“网络”,点击时会显示“没有应用程序可以执行此操作”
    猜你喜欢
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多