【问题标题】:Android Development RSS Feed does not workAndroid 开发 RSS Feed 不起作用
【发布时间】:2013-07-03 17:09:17
【问题描述】:

我正在阅读 Head First Android Development Book,这是一本很棒的书,但我在第二章(第 2 章),我无法让这个 RSS 应用程序正常工作。所以,基本上,这不应该是最终版本,但到目前为止它应该做的只是将应用程序显示为空。听起来很愚蠢,但它不应该显示任何东西,因为我必须为应用程序设置一些权限,以允许应用程序连接到互联网并下载 RSS 信息。我正在为这个应用程序使用 4 个不同的文件(但显然,项目中有更多文件)。

我制作了一个 Google Docs 文件夹,这样每个人都可以看到并下载它。我用的是 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 文件

请帮帮我,它快把我逼疯了!!!

谢谢

【问题讨论】:

标签: java android xml compiler-errors rss


【解决方案1】:

嘿,伙计,我感受到了你的痛苦我花了两天时间在那一章上找​​出错误的解决方案。 首先,这不是本书的完成版本,它充满了错误,所以不要难过,记住这一点。

强文本 以下是您的问题的解决方案。

在 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 扩展 Activity {

@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;
}

}

尽我所能

【讨论】:

  • 请再次检查您的答案。不要浪费别人的时间
【解决方案2】:

主要的问题是head first writer在对象命名方面有点不连贯。

  1. 未使用声明的对象。 (IotdHandler made, iotdHandler 使用。)java 和因此 android 区分大小写。
  2. 有时signature parameter of methods 与此不同 声明的方法。应该是(字符串,字符串,位图, StringBuffer),但调用者使用 (String, String, String, String)。

我建议您阅读 Wrox 或 Apress android 书籍 进行编程练习,然后前往 HeadFirst coding ideas to have robust code

【讨论】:

    【解决方案3】:

    我知道这篇文章已经很老了,但也许我的回答会帮助下一个可能会在这里找到自己的人,尤其是因为没有可接受的答案。

    如前所述,这本书充满了错误,可以让像我这样的 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 位于“附件”下。

    我的代码如下,有任何问题请留言;请注意,我也是一个初学者,所以很可能有更好的方法来做到这一点。

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

    如果你在模拟器中运行代码,请稍等片刻,我认为模拟器的网速比较慢。

    希望能帮助到那里的人。

    干杯。

    尼尔

    【讨论】:

      【解决方案4】:

      您必须在mainactivity.java 中调用resetDisplay() 方法的地方进行以下更正

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-11
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-18
        • 2011-05-15
        相关资源
        最近更新 更多