2013-03-23 108 views
0

我正在從頭第一個android工作在美國國家航空航天局圖像的一天的例子。在下面的最後一步代碼顯示錯誤。rss feed android example

public void resetDisplay(String title, String date, String imageUrl, String desc){ 

     TextView titleView = (TextView) findViewById(R.id.ImageTitle); 
     titleView.setText(title); 

     TextView dateView = (TextView) findViewById(R.id.ImageDate); 
     dateView.setText(date); 

     ImageView imgv = (ImageView) findViewById(R.id.ImageDisplay); 
     imgv.setImageBitmap(image); 


     TextView descView = (TextView) findViewById(R.id.ImageDesc); 
     descView.setText(desc); 
    } 

問題在於ImageView setter方法。什麼是圖像變量?任何人誰成功了這個例子請指導

任何鏈接,我可以找到這個例子的代碼將非常有幫助!

+0

請給我們看錯誤,我們無法猜測它。 – Elad92 2013-03-23 19:34:07

+0

錯誤:圖像無法解析爲變量。 本書中給出的代碼包含ImageUrl,它是String類型的參數。但是他們沒有在功能中使用它。函數使用可變圖像。我想知道,它從哪裏來! – srinath 2013-03-23 19:36:59

回答

1

你必須先下載圖像(從IMAGEURL):

URL url = new URL(stringURL); 
URLConnection conn = url.openConnection(); 
conn.connect(); 
InputStream is = conn.getInputStream(); 
BufferedInputStream bis = new BufferedInputStream(is); 
Bitmap image = BitmapFactory.decodeStream(bis); 

然後執行:

imgv.setImageBitmap(image); 
+0

這是一個很大的幫助。有什麼鏈接,我可以找到這個例子。或者至少是IotdHandler.java代碼? – srinath 2013-03-23 19:54:31

+0

我只是做了一個快速的谷歌搜索。爲什麼?它不工作嗎? – 2013-03-23 19:56:29

+0

我沒有得到任何編譯時錯誤。但是,在運行應用程序時,它顯示不幸地關閉應用程序〜:(並且我不確定該iotdhandler類。有人可以給我一個鏈接,我可以找到該類沒有任何錯誤 – srinath 2013-03-23 20:03:07

-1

公共類XmlHandler擴展了DefaultHandler {

private String url = "http://www.nasa.gov/rss/dyn/image_of_the_day.rss"; 

private boolean inUrl = false; 

private boolean inTitle = false; 

private boolean inDescription = false; 
private boolean inItem = false; 
private boolean inDate = false; 
private Bitmap image = null; 
private String imageUrl = null; 
private String title = null; 
private StringBuffer description = new StringBuffer(); 
private String date = null; 

public void processFeed() { 
    try { 
     // This part is added to allow the network connection on a main GUI 
     // thread... 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     SAXParserFactory factory = SAXParserFactory.newInstance(); 
     SAXParser parser = factory.newSAXParser(); 
     XMLReader reader = parser.getXMLReader(); 
     reader.setContentHandler(this); 
     URL urlObj = new URL(url); 
     InputStream inputStream = urlObj.openConnection().getInputStream(); 
     reader.parse(new InputSource(inputStream)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.out.println(new String("Got Exception General")); 
    } 
} 

private Bitmap getBitmap(String url) { 
    try { 
     System.out.println(url); 
     HttpURLConnection connection = (HttpURLConnection) new URL(url) 
       .openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap bitmap = BitmapFactory.decodeStream(input); 
     input.close(); 
     return bitmap; 
    } catch (IOException ioe) { 
     System.out.println(new String("IOException in reading Image")); 
     return null; 
    } catch (Exception ioe) { 
     System.out.println(new String("IOException GENERAL")); 
     return null; 
    } 
} 


public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 

    if (localName.equals("enclosure")) { 

     imageUrl = attributes.getValue("", "url"); 

     inUrl = true; 
    } else { 
     inUrl = false; 
    } 
    if (localName.startsWith("item")) { 
     inItem = true; 
    } else if (inItem) { 
     if (localName.equals("title")) { 

      inTitle = true; 
     } else { 
      inTitle = false; 
     } 
     if (localName.equals("description")) { 
      inDescription = true; 
     } else { 
      inDescription = false; 
     } 
     if (localName.equals("pubDate")) { 
      inDate = true; 
     } else { 
      inDate = false; 
     } 
    } 
} 


public void characters(char ch[], int start, int length) { 

    String chars = new String(ch).substring(start, start + length); 

    if (inUrl && image == null) { 



     image = getBitmap(imageUrl); 
    } 
    if (inTitle && title == null) { 


     title = chars; 
    } 
    if (inDescription && description == null) { 

     description.append(chars); 
    } 
    if (inDate && date == null) { 

     date = chars; 
    } 
} 

public Bitmap getImage() { 
    return image; 
} 

public String getTitle() { 
    return title; 
} 

public StringBuffer getDescription() { 
    return description; 
} 

public String getDate() { 
    return date; 
} 

}

+0

我也試過這本書的同樣的例子,我根據上面的代碼做了修改,所以這段代碼對我有用 – pareshm 2013-11-13 04:44:10

+0

從來沒有做過任何網絡主線程! – slinden77 2013-12-21 09:30:35