2013-07-03 65 views
0

我正在閱讀Head First Android Development Book,這是一本很棒的書,但我在第二章(第2章)中,我無法獲得這個RSS應用程序的工作。所以,基本上,這不應該是最終版本,但它迄今爲止所做的只是將應用程序視爲空白。聽起來很愚蠢,但它不應該顯示任何東西,因爲我必須爲應用程序設置一些權限才能允許應用程序連接到互聯網並下載RSS信息。我正在爲這個應用程序使用4個不同的文件(但很明顯,項目中有更多的文件)。Android Development RSS Feed does not work

我製作了一個Google文件夾,這樣每個人都可以看到它並下載它。我使用Eclipse。

請幫助我這是一本書,但我找不到下一章,直到找到解決方案。

再次,這不應該是最終版本,該應用程序應該被視爲空,因爲我需要爲它設置一些權限。它給了我錯誤,請幫助我!

我的主要問題是這行,它說「iotdHandler無法解決」。我不知道爲什麼這本書說我不應該大寫這個詞我想我應該喜歡「IotdHandler」,但它仍然給我錯誤。我遵循了書中的所有內容。幫我!

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    IotdHandler handler = new IotdHandler(); 
    handler.processFeed(); 
    resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(), iotdHandler.getImage(), iotdHandler.getDescription()); 
} 

此代碼是從mainActivity.java文件

請幫助我,這是快把我逼瘋了!

感謝

+0

忘了RSS源。 http://www.nasa.gov/rss/image_of_the_day.rss – user2547460

回答

0

man。我覺得你的痛苦我花了兩天時間在這一章找出了錯誤的解決方案。 拳頭,最重要的是這不是書的完成版本,它是困擾着錯誤,所以不要覺得不好,並牢記在心。

strong text 以下是您的問題的解決方案。

在mainActivity.java

package com.example.nasadailyimage; 

import android.iotdHandler; 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.view.Menu; 
import android.widget.ImageView; 
import android.widget.TextView; 

公共類MainActivity擴展活動{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    IotdHandler handler = new IotdHandler(); //create handler 
    handler.processFeed(); //start parsing 
    resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(), 
      iotdHandler.getImage(), iotdHandler.getDescription()); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

private void resetDisplay(String title, String date, String imageUrl, String description) 
{ 
    TextView titleView = (TextView)findViewById(R.id.imageTitle); 
    titleView.setText(title); 

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

    ImageView imageView = (ImageView)findViewById(R.id.imageDisplay); 
    Bitmap image = null; 
    imageView.setImageBitmap(image); 

    TextView descriptionView = (TextView)findViewById(R.id.imageDescription); 
    descriptionView.setText(description); 

} 

}

其次:你需要在你的src創建一個名爲IotdHandler.java文件夾。 在這個文件中,你需要創建以下getter方法

package android; 

public class iotdHandler { 

public static String getDate() { 
    // TODO Auto-generated method stub 
    return null; 
} 
public static String getTitle() { 
    // TODO Auto-generated method stub 
    return null; 
} 
public static String getImage() { 
    // TODO Auto-generated method stub 
    return null; 
} 
public static String getDescription() { 
    // TODO Auto-generated method stub 
    return null; 
} 

}

我所能

+0

請再次檢查您的答案。不要浪費別人的時間 – sar

0

的主要問題是,頭先作家是對象而言有點語無倫次命名爲

  1. 什麼對象聲明沒有使用。 (IotdHandler made, iotdHandler used。)java和因此android區分大小寫。
  2. 有時signature parameter of methods不同於所聲明方法的那個 。它應該是(String,String,Bitmap, StringBuffer),但調用者使用(String,String,String,String)。

我建議你讀做編程練習Wrox的或Apress出版的Android書籍,然後去HeadFirst coding ideas to have robust code

-1

你必須要到位以下修正,你在你的mainactivity.java

resetDisplay(handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription()); 
0

調用resetDisplay()方法,我知道這個職位是比較舊的,但也許我的回答將幫助旁邊的人可能在這裏找到自己,特別是因爲沒有被接受的答案。

如上所述,本書充滿了錯誤,可以讓android和java的新手像我一樣,有點瘋狂。經過幾天的谷歌搜索和閱讀,我設法讓這個工作,所以在這裏。

我不確定OP使用的是什麼版本的Android,但我認爲本書中的代碼存在的問題之一是來自Honeycomb(3.x)和以上版本的附加問題,不允許使用昂貴在UI線程上的操作。瞭解更多關於它:

http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html 

對於這個問題,你需要使用的AsyncTask一個單獨的線程運行潛在的昂貴的操作。

請注意,在捕獲異常的代碼中,我只是將異常錯誤打印到titleView TextView中,以便更容易地找出問題所在。

運行完AsyncTask中的代碼後,更新顯示。

startElement方法似乎也不正確,因爲如果您在XML編輯器中查看RSS源詳細信息,則圖像所需的URL位於「enclosure」下。

我的代碼如下,請留下任何問題的意見;請注意我也是一個初學者,所以這可能是一個更好的方法。

package neill.nasadailyimage; 

import java.io.*; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import javax.xml.parsers.*; 

import org.xml.sax.*; 
import org.xml.sax.helpers.DefaultHandler; 

import android.support.v7.app.ActionBarActivity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class NasaDailyImage extends ActionBarActivity 
{ 
    IotdHandler handler = new IotdHandler(); // Create handler 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_nasa_daily_image); 

     handler.processFeed();  
    } 

    public void ResetDisplay() { 

     String title = handler.getTitle(); 
     String date = handler.getDate(); 
     String description = handler.getDescription().toString(); 

     resetDisplay(title, date, handler.getImage(), description); 
    } 

    private void resetDisplay(String title, String date, Bitmap image, String description) 
    { 
     try { 

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

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

      ImageView imageView = (ImageView)findViewById(R.id.imageDisplay); 
      imageView.setImageBitmap(image); 

      TextView descriptionView = (TextView)findViewById(R.id.imageDescription); 
      descriptionView.setText(description); 

      } catch (Exception e) { 
       TextView titleView = (TextView) findViewById(R.id.imageTitle); 
       titleView.setText(e.toString()); 
      } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.nasa_daily_image, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public class IotdHandler extends DefaultHandler 
    { 
     private String url = "http://www.nasa.gov/rss/image_of_the_day.rss"; 

     private boolean inTitle = false; 
     private boolean inDescription = false; 
     private boolean inItem = false; 
     private boolean inDate = false; 

     private Bitmap image = null; 
     private String title = null; 
     private String date = null; 
     private StringBuffer description = new StringBuffer(); 

     public XMLReader reader = null; 

     private Bitmap getBitmap(String url) 
     { 
      try 
      { 
       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) 
      { 
       TextView titleView = (TextView) findViewById(R.id.imageTitle); 
       titleView.setText(ioe.toString()); 

       return null; 
      } 
     } 

     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 
     { 
       if (localName.equals("enclosure")) { 
        image = getBitmap(attributes.getValue("url").toString()); 
       } 

       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 (inTitle && title == null) { title = chars; } 

       if (inDescription) { description.append(chars); } 

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

     } 

     private class ProcessFeedTask extends AsyncTask<String, Void, InputStream> 
     {  
      @Override 
      protected InputStream doInBackground(String... params) 
      { 
       String url = params[0]; 

       InputStream inputStream = null; 

       try 
       {   
        inputStream = new URL(url).openStream(); 

        reader.parse(new InputSource(inputStream)); 

       } 
       catch (Exception e) 
       { 
        TextView titleView = (TextView) findViewById(R.id.imageTitle); 
        titleView.setText(e.toString()); 
       } 

       return inputStream; 
      } 

      @Override 
      protected void onPostExecute(InputStream result) 
      { 
       super.onPostExecute(result); 

       if (result != null) { 
        ResetDisplay(); 
       } 
      } 
     } 

     public void processFeed() 
     { 
      try 
      { 
       SAXParserFactory factory = SAXParserFactory.newInstance(); 

       SAXParser parser = factory.newSAXParser(); 

       reader = parser.getXMLReader(); 
       reader.setContentHandler(this); 

       new ProcessFeedTask().execute(url); 

      } 
      catch (Exception e) 
      { 
       TextView titleView = (TextView) findViewById(R.id.imageTitle); 
       titleView.setText(e.toString()); 
      } 
     } 

     public String getTitle() { return title ; } 
     public String getDate() { return date ; } 
     public String getDescription() { return description.toString() ; } 
     public Bitmap getImage() { return image ; } 
    } 
} 

如果你在運行一個仿真器的代碼,給它一段時間的工作,我覺得仿真器的上網速度比較慢。

希望能幫助那裏的人。

乾杯。

尼爾