【发布时间】:2013-04-22 01:06:27
【问题描述】:
我正在尝试通过 Android 中的 a 按钮打开网页,但无法正常工作。我找到了一个看起来非常简单的教程,但诀窍是我试图显示一个从另一个活动类中创建的对象中获取的 url。我使用的代码是标准的 OnClickListener:
private void addListeneronButton() {
// TODO Auto-generated method stub
button1 = (Button) findViewById(R.id.stationHistory);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse(//need help here));
startActivity(browserIntent);
}
该项目正在读取气象站列表。我有一个带有适当的 getter/setter 的 Station 类,所以 Url 已经存储了。我只是不知道从这里访问的适当命令。我认为它会像 Uri.parse(station.get_url) 一样简单,但它只会提示我创建局部变量...有人有什么建议吗?
<station>
<station_id>NFNA</station_id>
<state>FJ</state>
<station_name>Nausori</station_name>
<latitude>-18.05</latitude>
<longitude>178.567</longitude>
<html_url>http://weather.noaa.gov/weather/current/NFNA.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/NFNA.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/NFNA.xml</xml_url>
</station>
<station>
<station_id>KCEW</station_id>
<state>FL</state>
<station_name>Crestview, Sikes Airport</station_name>
<latitude>30.79</latitude>
<longitude>-86.52</longitude>
<html_url>http://weather.noaa.gov/weather/current/KCEW.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/KCEW.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/KCEW.xml</xml_url>
</station>
<station>
<station_id>KDTS</station_id>
<state>FL</state>
<station_name>Destin, Ft. Walton Beach Airport</station_name>
<latitude>30.4</latitude>
<longitude>-86.47</longitude>
<html_url>http://weather.noaa.gov/weather/current/KDTS.html</html_url>
<rss_url>http://weather.gov/xml/current_obs/KDTS.rss</rss_url>
<xml_url>http://weather.gov/xml/current_obs/KDTS.xml</xml_url>
</station>
我将这些解析为带有构造函数/getter/setter 的 Station 类。从先前的活动中,用户选择这些站之一。在下一个活动中,它会显示此 xml 中的名称、id 等元素。但现在我想通过一个按钮打开电台网址,但我不确定如何。
【问题讨论】:
-
粘贴网址,注意需要在Uri.parse中包含
http://或https:// -
所以试着把
http://放在前面:http://http://weather.noaa.gov/weather/current/NFNA.html -
但是我从上面发布的示例中得到了这些。
-
我不知道为什么这行不通,但是您是否尝试过创建像
Uri uri = http:// weather.noaa.gov/weather/current/NFNA.html这样的变量,然后将其放入您的Intent?
标签: java android url webview onclicklistener