0

我正在尝试从 Android 中的一个按钮打开一个网页,但无法让它正常工作。我找到了一个看起来非常简单的教程,但诀窍是我试图显示一个从另一个活动类中创建的对象中获取的 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>

我将这些解析为带有构造函数/获取器/设置器的站类。从先前的活动中,用户选择这些站之一。在下一个活动中,它会显示此 xml 中的名称、id 等元素。但现在我想从一个按钮打开电台网址,但我不知道如何。

4

2 回答 2

2

我想我明白你说的是现在的问题,虽然你真的没有说得很清楚。你有这个

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(station.get_url);
        startActivity(browserIntent);

    }

它告诉你这station.get_url不是一个变量,所以你需要创建一个。如果它是一种方法,get_url应该是。get_url()如果您的班级是Station(大写“S”),那么您可以使用Station.get_url(). 但是,您可能需要传入某个参数来告诉它要获取哪个 url。这是假设它是一种static方法。如果不是,那么您需要创建Station该类的一个实例并调用该方法

Station myStation = new Station();  //pass parameters here to constructor as needed
String address = myStation.get_url();  // object

然后你可以address在你的Intent.

 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(address));
 startActivity(browserIntent);

如果http://没有与 url 一起传回,则必须在使用它之前附加它Intent,正如 wangyif2 已经说明的那样。我希望这是有道理的,但这是我能用所提供的信息做的最好的事情。

于 2013-04-22T01:56:27.420 回答
1

在进行 URI 解析时,尝试将 http:// 放在字符串之前:

private void addListeneronButton() {
   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(Station.getHtmlUrl("NFNA"));
           startActivity(browserIntent);
       }
}

车站等级:

public static String getHtmlUrl(String station) { 
    //return the full URL here based on station 
    //eg. "http://weather.noaa.gov/weather/current/NFNA.html"
}
于 2013-04-22T01:17:05.773 回答