Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6661095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:14:13+00:00 2026-05-26T02:14:13+00:00

I’ve been trying to set up a UI Thread and Handler for a SAX

  • 0

I’ve been trying to set up a UI Thread and Handler for a SAX parser. This is my parser without a UI Thread and Handler implemented:

public class AndroidXMLReader extends ListActivity {

    private XMLFeed myXMLFeed = null;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
            try {
                URL xmlUrl = new URL("http://feeds.bbci.co.uk/news/uk/rss.xml");
                SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
                SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
                XMLReader myXMLReader = mySAXParser.getXMLReader();
                XMLHandler myXMLHandler = new XMLHandler();
                myXMLReader.setContentHandler(myXMLHandler);
                InputSource myInputSource = new InputSource(xmlUrl.openStream());
                myXMLReader.parse(myInputSource);

                myXMLFeed = myXMLHandler.getFeed();

            }

        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (myXMLFeed!=null)
        {
            ArrayAdapter<XMLItem> adapter =
             new ArrayAdapter<XMLItem>(this,
               android.R.layout.simple_list_item_1,myXMLFeed.getList());
            setListAdapter(adapter);
        }
        }
    }

Then here I went and tried to add the UI Thread & Handler. Edit 1 – this is the amended code as per HellBoy’s reply:

   public class AndroidXMLReader extends ListActivity {

    private static final int THREAD_FINISHED = 0;
    private XMLFeed myXMLFeed = null;
    private ProgressDialog progressDialog;
    private Button refreshFeed;
    Handler handler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initControls();
    }

    public void initControls(){


          refreshFeed = (Button) findViewById(R.id.refresh);

          refreshFeed.setOnClickListener(new Button.OnClickListener(){
              @Override
              public void onClick(View v){
                  progressDialog = ProgressDialog.show(AndroidXMLReader.this, "",
                            "Please wait for few seconds...", true);
                  processThread();          
              }
          });
    }
    protected void processThread() {
        Thread t = new Thread()
        {
            public void run(){
                 getNews();
                 //UI();
                 // Sends message to the handler so it updates the UI
                 handler.sendMessage(Message.obtain(mHandler, THREAD_FINISHED));

                 ; }
        };
        t.start();
    }

    private Handler mHandler = new Handler() {
        @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                case THREAD_FINISHED:

                           progressDialog.dismiss();
                           break;    
                    };

            };
    };

    private void getNews(){
        try {
            URL xmlUrl = new URL("http://feeds.bbci.co.uk/news/uk/rss.xml");
            SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
            SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
            XMLReader myXMLReader = mySAXParser.getXMLReader();
            XMLHandler myXMLHandler = new XMLHandler();
            myXMLReader.setContentHandler(myXMLHandler);
            InputSource myInputSource = new InputSource(xmlUrl.openStream());
            myXMLReader.parse(myInputSource);

            myXMLFeed = myXMLHandler.getFeed();

        }

        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (myXMLFeed!=null){
            ArrayAdapter<XMLItem> adapter =
                 new ArrayAdapter<XMLItem>(this,
                   android.R.layout.simple_list_item_1,myXMLFeed.getList());
                setListAdapter(adapter);
        }

    }
}

At the moment it hangs at the progress dialog and I can’t figure out why … Any help would be appreciated.

Edit 1 – The error message that I get now is:

Only the original thread that created a view hierarchy can touch its
views.

    10-04 21:39:27.570: ERROR/AndroidRuntime(17454):     at com.android.testfeed3.AndroidXMLReader.getNews(AndroidXMLReader.java:121)
10-04 21:39:27.570: ERROR/AndroidRuntime(17454):     at com.android.testfeed3.AndroidXMLReader.access$2(AndroidXMLReader.java:85)
10-04 21:39:27.570: ERROR/AndroidRuntime(17454):     at com.android.testfeed3.AndroidXMLReader$3.run(AndroidXMLReader.java:60)
10-04 21:39:27.645: WARN/ActivityManager(2703):   Force finishing activity com.android.testfeed3/.AndroidXMLReader
10-04 21:39:33.495: ERROR/WindowManager(17454): Activity com.android.testfeed3.AndroidXMLReader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40527a20 that was originally added here
10-04 21:39:33.495: ERROR/WindowManager(17454): android.view.WindowLeaked: Activity com.android.testfeed3.AndroidXMLReader has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40527a20 that was originally added here
10-04 21:39:33.495: ERROR/WindowManager(17454):     at com.android.testfeed3.AndroidXMLReader$2.onClick(AndroidXMLReader.java:50)

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:14:13+00:00Added an answer on May 26, 2026 at 2:14 am

    In order to make changes to the UI the commands must come from thread that created it, i.e. the main (or UI) thread.

    At a quick glance, this line:

    if (myXMLFeed!=null)
    {
       ArrayAdapter<XMLItem> adapter =
        new ArrayAdapter<XMLItem>(this,
          android.R.layout.simple_list_item_1,myXMLFeed.getList());
       setListAdapter(adapter);
    }
    

    needs to be put in a separate Handler and that Handler called from your new thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.