引起:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图

时间:2013-07-04 10:22:49

标签: android

我正在尝试编写一个程序来解析给定的xml。但是,它会抛出错误消息:“引起:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。” 这是我的代码

    package com.example.xmlparserdeneme;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

    ArrayList<String> titles = new ArrayList<String>();
    Context ctx = this;
    CustomAdapter adapter = null;
    ListView lv;

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

        new parseXML().execute();

        Log.i("info2", titles.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.main, menu);
        return true;
    }

    class parseXML extends AsyncTask<String, Void, String> {

        MySaxHandler handler;

        @Override
        protected String doInBackground(String... params) {

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser parser = null;
            try {
                parser = spf.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }

            handler = new MySaxHandler();

            try {
                parser.parse(
                        "/xmlParserDeneme/src/Database.xml",
                        handler);
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Log.i("info", titles.toString());
            return "a";

        }

        @Override
        protected void onPostExecute(String result) {

            lv = (ListView) findViewById(R.id.listView);
            lv.setAdapter(new CustomAdapter(titles, ctx));
        }

        public ArrayList<String> getArray() {
            return titles;
        }

        public class MySaxHandler extends DefaultHandler {

            StringBuffer chars;

            public void startElement(String uri, String localName,
                    String qName, Attributes atts) {
                chars = new StringBuffer();

            }

            public void endElement(String uri, String localName, String qName)
                    throws SAXException {

                if (localName.equalsIgnoreCase("description"))
                    titles.add(chars.toString());

            }

            public void characters(char ch[], int start, int length) {
                chars.append(new String(ch, start, length));
            }

        }

    }
}

我是android的新手,我不知道在哪里改变。 如果有人告诉我如何解决它,我将不胜感激?

1 个答案:

答案 0 :(得分:1)

请在oncreate方法中编写此代码。

ctx = this;

删除此代码。

((Activity)ctx)。setContentView(R.layout.activity_main);