为什么我看不到观看次数?

发布于 2024-12-27 12:59:49 字数 2592 浏览 0 评论 0原文

我有下一个代码,

     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);
    }

当我想要获取视图时, 我收到此错误:只有创建视图层次结构的原始线程才能触摸其视图。
有人有这个代码的解决方案吗?
PSD:这是一个 rss 解析

I have the next code

     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);
    }

when i want to get the View I got this error: Only the original thread that created a view hierarchy can touch its views.

anybody have a solution for this code?

PSD: this is a rss parse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2025-01-03 12:59:49

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

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

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

You cant update the UI on the a seperate thread. You only can update it on the Main thread

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

For this you should use an AsnycTask. It has methods that allow you to update the main UI while doing background work, or after or before it is complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文