为什么我没有获得意见?

时间:2012-01-16 17:17:57

标签: android views progressdialog

我有下一个代码

     ProgressDialog pd;
 LinearLayout layout;
 ListView listview;
 TextView name[];
 TextView website[];
/** Called when the activity is first created. 
 */

@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main); 
       pd = ProgressDialog.show(this,
               "Title",
               "Message",
               true, false);

       new Thread(new Runnable(){
           public void run(){
           begin();
           pd.dismiss();
           }
           }).start();







    }




public void begin(){       
    Calendar c = Calendar.getInstance(); 

    String dia2,mes2;
    mes2="0";
   int dia = c.get(Calendar.DAY_OF_MONTH); 
   int mes = c.get(Calendar.MONTH); 




    try {

    URL url = new URL(
    "http://www.tudiscovery.com/dni-tvlistings/GetScheduleByBroadcastDate?type=day&country_code=LTM&channel_code=DCLA-SP&date="+ dia2 +""+ mes2 +"2012");



    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));

    doc.getDocumentElement().normalize();

    NodeList nodeList = doc.getElementsByTagName("programme");

    /** Assign textview array lenght by arraylist size */
    name = new TextView[nodeList.getLength()];
    website = new TextView[nodeList.getLength()];


    for (int i = 0; i <= nodeList.getLength(); i++) {

    Node node = nodeList.item(i);

    name[i] = new TextView(this);
    website[i] = new TextView(this);


    Element fstElmnt = (Element) node;
    NodeList nameList = fstElmnt.getElementsByTagName("series-title");
    Element nameElement = (Element) nameList.item(0);
    nameList = nameElement.getChildNodes();

    name[i].setText("Programa = "
    + ((Node) nameList.item(0)).getNodeValue());

    NodeList websiteList = fstElmnt.getElementsByTagName("raw");
    Element websiteElement = (Element) websiteList.item(0);
    websiteList = websiteElement.getChildNodes();
    website[i].setText("Hora = "
    + ((Node) websiteList.item(0)).getNodeValue().subSequence(0, 2) +":" + ((Node) websiteList.item(0)).getNodeValue().subSequence(2, 4));


    name[i].setTextColor(Color.RED);
    name[i].setTextSize(12);
   layout.addView(name[i]);
    layout.addView(website[i]);



    } 


    } catch (Exception e) {
    System.out.println("XML Pasing Excpetion = " + e);
    }

当我想获取View时出现此错误:只有创建视图层次结构的原始线程才能触及其视图。
任何人都有这个代码的解决方案?
PSD:这是一个rss解析

1 个答案:

答案 0 :(得分:2)

您无法在单独的线程上更新UI。您只能在主线程

上更新它
 new Thread(new Runnable(){
       public void run(){
       begin();
       pd.dismiss();
       }
       }).start();

为此你应该使用AsnycTask。它具有允许您在进行后台工作时或在完成后或之前更新主UI的方法。