【问题标题】:Linkify an address [closed]链接地址[关闭]
【发布时间】:2014-10-26 18:53:43
【问题描述】:

下面的代码有问题

setContentView(R.layout.activity_main);
    TextView myLocation = (TextView)findViewById(R.id.txtAddress);
    myLocation.setText("123 Main St New York, NY");
    Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES);
    mainLayout.addView(myLocation);    

我的问题在于说的部分

mainLayout.addView(myLocation);

它抱怨“mainLayout 无法解决”,这正是我在引号中所说的错误。

有人对如何解决这个问题有建议吗?

【问题讨论】:

  • 您可以编辑问题并准确粘贴错误消息的内容吗?
  • 确切的错误是 mainLayout cannot be resolved
  • 您需要从 XML 中的 id 属性中找到您的布局“mainLayout”,setContentView 之后,mainLayout.addView(myLocation) 之前;所以 mainLayout 可以解决 - 就像你为 TextView myLocation 所做的那样 - 但应该为布局完成 - RelativeLayout 或 LinearLayout - 无论你在 XML 中设置了什么
  • 你能举个例子吗?
  • 如果对我有一个 RelativeLayout 有帮助

标签: java android linkify


【解决方案1】:

不确定为什么要再次将 textview 添加到布局中。您已经用地址更新了文本视图。如果删除 addView 行,错误就会消失。无论如何,这是如何消除错误“mainLayout cannot be resolved”:从 onCreate 中处理的 XML 中的 id 属性中找到布局“mainLayout”的视图 - 请参阅添加的行:

setContentView(R.layout.activity_main);
    // add
    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.layoutnameinXML);

    TextView myLocation = (TextView)findViewById(R.id.txtAddress);
    myLocation.setText("123 Main St New York, NY");
    Linkify.addLinks(myLocation , Linkify.MAP_ADDRESSES);
    mainLayout.addView(myLocation); 

在 XML 中你应该给 RelativeLayout 一个 id 属性,比如:

<RelativeLayout        
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layoutnameinXML"  >

【讨论】:

  • 谢谢它摆脱了那个错误但现在它没有给出错误并且在启动时它崩溃了我能够将它缩小到 mainLayout.addView(myLocation)当我注释掉该部分代码时,程序启动但地址不可点击,这意味着它只是文本未链接,但是当该部分代码未注释掉时,它会在启动时崩溃程序。
  • 你能把你在注释掉 addView 行后得到的崩溃的 logcat 贴出来吗?
  • 地址在 City 和 State 之前应该有一个逗号。例如:123 Main Street, New York, NY - 或尝试 Linkify.addLinks(myLocation , Linkify.ALL);用于自动检测。
  • 我无法发布逻辑,因为它超出了允许的字符。
  • 您是否尝试按照我上面的最后评论更正地址或使用 linkify.all - 您需要了解有关如何使用 linkify 的更多信息 - 请不要忘记将我的答案标记为已接受最初的问题。
猜你喜欢
  • 1970-01-01
  • 2016-11-06
  • 2014-01-29
  • 1970-01-01
  • 2017-11-29
  • 2016-05-13
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多