Question

I am creating an RSS reader application for android 4.0+. I have put the reader code in AsyncTask because of the NetworkOnMainThreadException. The code almost works fine, however one line has an error. This is my code:

Java code:

   private class PostTask extends AsyncTask<String, Integer, String> {
   @Override
   protected void onPreExecute() {
   super.onPreExecute();
   }

   @Override
   protected String doInBackground(String... params) {
  String url=params[0];
   headlines = new ArrayList();
   links = new ArrayList();

   //Download and parse xml feed
   headlines = new ArrayList();
   links = new ArrayList();

   try {
    URL url1 = new URL("http://feeds.pcworld.com/pcworld/latestnews");

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(false);
    XmlPullParser xpp = factory.newPullParser();

           // We will get the XML from an input stream
    xpp.setInput(getInputStream(url1), "UTF_8");

           /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
            * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
            * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
            * so we should skip the "<title>" tag which is a child of "<channel>" tag,
            * and take in consideration only "<title>" tag which is a child of "<item>"
            *
            * In order to achieve this, we will make use of a boolean variable.
            */
    boolean insideItem = false;

           // Returns the type of current event: START_TAG, END_TAG, etc..
    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {

            if (xpp.getName().equalsIgnoreCase("item")) {
                insideItem = true;
            } else if (xpp.getName().equalsIgnoreCase("title")) {
                if (insideItem)
                    headlines.add(xpp.nextText()); //extract the headline
            } else if (xpp.getName().equalsIgnoreCase("link")) {
                if (insideItem)
                    links.add(xpp.nextText()); //extract the link of article
            }
        }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
            insideItem=false;
        }

        eventType = xpp.next(); //move to next element
    }

   } catch (MalformedURLException e) {
    e.printStackTrace();
   } catch (XmlPullParserException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }

  return "Feed parsed!";
   }

   @Override
   protected void onProgressUpdate(Integer... values) {
  super.onProgressUpdate(values);
   }

   @Override
   protected void onPostExecute(String result) {
  super.onPostExecute(result);
   // Binding data
  ArrayAdapter adapter = new ArrayAdapter(this,
          android.R.layout.simple_list_item_1, headlines);
  setListAdapter(adapter);

   }
   }

in this code snippet i get an error:

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Binding data
ArrayAdapter adapter = new ArrayAdapter(this,
          android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}

Error: The constructor ArrayAdapter(MainActivity.PostTask, int, List) is undefined.

Logcat:

01-26 20:55:53.811: E/AndroidRuntime(1830): FATAL EXCEPTION: AsyncTask #1
01-26 20:55:53.811: E/AndroidRuntime(1830): java.lang.RuntimeException: An error occured while executing doInBackground()
01-26 20:55:53.811: E/AndroidRuntime(1830):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.lang.Thread.run(Thread.java:856)
01-26 20:55:53.811: E/AndroidRuntime(1830): Caused by: java.lang.IllegalArgumentException
01-26 20:55:53.811: E/AndroidRuntime(1830):     at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1615)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at com.mysoftware.mysoftwareos.mobile.MainActivity$PostTask.doInBackground(MainActivity.java:343)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at com.mysoftware.mysoftwareos.mobile.MainActivity$PostTask.doInBackground(MainActivity.java:1)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-26 20:55:53.811: E/AndroidRuntime(1830):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-26 20:55:53.811: E/AndroidRuntime(1830):     ... 5 more

Could anyone please look into my problem and come up with a solution? Or tell me if i should do anything different? Thanks a lot!

Was it helpful?

Solution 2

use Activity Context instead of AsyncTask to Create ArrayAdapter inside onPostExecute method as :

ArrayAdapter<String> adapter = 
                      new ArrayAdapter<String>(Your_Current_Activity.this,
                      android.R.layout.simple_list_item_1, headlines);
  setListAdapter(adapter);

EDIT : after log posted also change

xpp.setInput(getInputStream(url1), "UTF_8");

to

xpp.setInput(url1.openConnection().getInputStream(), "UTF_8");

OTHER TIPS

Change it to:

ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
      android.R.layout.simple_list_item_1, headlines);

this refers to the AsyncTask, so you have to explicitly reference MainActivity's this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top