2013-07-04 83 views
0

我想編寫一個程序來解析給定的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新手,我不知道要在哪裏改變。 如果有人告訴我如何解決問題,我將不勝感激?

+0

當你從另一個線程更新ui時會發生這種情況。還要刪除這個'((Activity)ctx).setContentView(R.layout.activity_main)'。 – Raghunandan

+0

也是什麼''/xmlParserDeneme/src/Database.xml「'你的Database.xml在哪裏? – Raghunandan

+1

@拉康丹丹,我的數據庫是一個plist文件。 – anilbey

回答

1

請將此代碼寫入oncreate方法。

ctx = this;

並刪除此代碼。

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

+1

謝謝,它解決了我的問題。現在我有另一個問題,你會看看它嗎? [link] http://stackoverflow.com/questions/17471956/my-xml-parsersax-parser-parses-only-uri-source-how-can-i-parse-a-file-which-e [link ] – anilbey

相關問題