2013-04-22 4 views
0

Android의 버튼에서 웹 페이지를 열려고하는데 작동하지 않습니다. 나는 매우 직설적 인 튜토리얼을 발견했지만 트릭은 다른 활동 클래스에서 작성된 오브젝트에서 가져온 URL을 표시하려고 시도한다는 것입니다. 제가 사용하는 코드는 표준 OnClickListener입니다 :OnClickListener에서 URL 열기 Android

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); 

     } 

프로젝트는 기상 관측소 목록을 읽습니다. URL이 이미 저장되도록 적절한 getters/setter가있는 Station 클래스가 있습니다. 나는 여기에서 접근 할 적절한 명령을 모른다. 나는 그것이 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> 

나는 이것을 생성자/getters/setters가있는 Station Class로 구문 분석합니다. 이전 활동에서 사용자는이 스테이션 중 하나를 선택합니다. 다음 활동에서이 XML의 이름, id 등의 요소를 표시합니다. 하지만 이제는 단추로 방송국 URL을 열고 싶습니다. 어떻게해야할지 모르겠습니다.

+0

Uri.parse에 'http : //'또는 'https : //'를 포함해야한다는 점에 유의하십시오. – wangyif2

+0

샘플 URL은 http://weather.noaa.gov/weather/입니다. current/NFNA.html – David

+0

그래서 http : //를 입력 해보십시오. http : // http : // http : // weather.noaa.gov/weather/current/NFNA.html' – wangyif2

답변

1

시도 퍼팅 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); 
     } 
} 

역 클래스 : // 당신이 당신의 URI 구문 분석 할 문자열 전에 내가 당신이 말하는 것은 이제 문제가 볼 생각

public static String getHtmlUrl(String station) { 
    //return the full URL here based on station 
    //eg. "http://weather.noaa.gov/weather/current/NFNA.html" 
} 
+0

위의 내 의견을 편집했습니다. – David

+0

다시 확인하십시오. 원하는게 맞습니까? – wangyif2

1

을, 당신이 정말로 그것을 분명하게하지는 않았지만. 이

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 myStation = new Station(); //pass parameters here to constructor as needed 
String address = myStation.get_url(); // object 

그런 다음 당신은 당신의 Intentaddress를 사용 할 수있는 방법이에게 Station 클래스의 인스턴스를 생성하고 호출해야합니다. http://가 URL로 다시 전달되지 않는 경우

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

는이 wangyif2 이미 언급 한 바와 같이, Intent에서 사용하기 전에 추가해야합니다. 이것이 의미가 있지만 희망하는 정보로 최선을 다할 수 있기를 바랍니다.