簡體   English   中英

SetText不可能,因為線程錯誤

[英]SetText Impossible because bad thread

我開始使用android應用程序,但遇到了一些問題。 我的應用程序包含HTML頁面的一部分,並將此HTML的一些文本放入Textviews中。 錯誤是:“只有創建視圖層次結構的原始線程才能觸摸其視圖”。 如果我不了解,則意味着我應該將“ setText()”放在onCreate()函數中,但由於HTML尚未完全加載,因此它也不起作用。

對不起,我的英語不好,謝謝您的回答。

package com.example.goo;

import java.text.Normalizer;
import java.util.Arrays;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;


@SuppressLint("NewApi")
public class Classement extends Activity{

    Document doc;
    String code;

    static TextView Club1;
    static TextView Club2;
    static TextView Club3;
    static TextView Club4;
    static TextView Club5;
    static TextView Club6;
    static TextView Club7;
    static TextView Club8;
    static TextView Club9;
    static TextView Club10;
    static TextView Club11;
    static TextView Club12;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classement);

        //Attribution des TextView
        this.Club1  = (TextView)findViewById(R.id.TVClub1);
        this.Club2  = (TextView)findViewById(R.id.TVClub2);
        this.Club3  = (TextView)findViewById(R.id.TVClub3);
        this.Club4  = (TextView)findViewById(R.id.TVClub4);
        this.Club5  = (TextView)findViewById(R.id.TVClub5);
        this.Club6  = (TextView)findViewById(R.id.TVClub6);
        this.Club7  = (TextView)findViewById(R.id.TVClub7);
        this.Club8  = (TextView)findViewById(R.id.TVClub8);
        this.Club9  = (TextView)findViewById(R.id.TVClub9);
        this.Club10  = (TextView)findViewById(R.id.TVClub10);
        this.Club11  = (TextView)findViewById(R.id.TVClub11);
        this.Club12  = (TextView)findViewById(R.id.TVClub12);

        //Appel de la classe gérant le HTTP
        new NetworkOperation().execute();
    }   

    //Gère le HTTP
    class NetworkOperation extends AsyncTask<Void, Void, String > {
        protected String doInBackground(Void... params) {
            //On se connecte au site et on charge le document html grâce au plugin Jsoup
            try {
                System.out.println("1");
                doc = Jsoup.connect("http://www.nationalleague.ch/NL/fr/index.php").get();
                System.out.println("2");
                //Récupère le texte d'une balise ayant tblAdvA pour id
                Element getId = doc.getElementById("tblAdvA");
                code = getId.text();
                System.out.println(code);

                //Supprime les accents
                code = Normalizer.normalize(code, Normalizer.Form.NFD);
                code = code.replaceAll("[^\\p{ASCII}]", "");

                //Appelle la fonction permettant de donner le classement
                Classement.splitText(code);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    //Splitte le texte
    public static String [] splitText(String text){

        int [] tabInt = new int [12];
        String [] tabString = new String [12]; 

        tabString[0]        = "" + text.indexOf("Fribourg", 0) + "Fribourg";
        tabString[1]        = "" + text.indexOf("SC Bern", 0)  + "Bern";
        tabString[2]        = "" + text.indexOf("EV Zug", 0)   + "Zug";
        tabString[3]        = "" + text.indexOf("ZSC", 0)      + "ZSC";
        tabString[4]        = "" + text.indexOf("HC Davos", 0) + "Davos";
        tabString[5]        = "" + text.indexOf("Lugano", 0)   + "Lugano";
        tabString[6]        = "" + text.indexOf("Geneve", 0)   + "Geneve";
        tabString[7]        = "" + text.indexOf("Biel", 0)     + "Bienne";
        tabString[8]        = "" + text.indexOf("Kloten", 0)   + "Kloten";
        tabString[9]        = "" + text.indexOf("Ambri", 0)    + "Ambri";
        tabString[10]       = "" + text.indexOf("Lakers", 0)   + "Lakers";
        tabString[11]       = "" + text.indexOf("SCL", 0)      + "SCL";

        //Extraction des entiers
        for (int i = 0; i < tabString.length; i++) {
            tabInt[i] = Integer.parseInt(tabString[i].replaceAll("[a-zA-Z]", ""));
        }

        //Tri du tableau des entiers
        Arrays.sort(tabInt);

        //Sort le Classement exact
        for (int j = 0; j < tabString.length; j++) {
            for (int i = 0; i < tabString.length; i++) {
                if (tabInt[i] == Integer.parseInt(tabString[j].replaceAll("[a-zA-Z]", ""))) {
                    System.out.println(tabString[j].replaceAll("[\\d]", ""));
                }
            }
        }

        //Edite Classement
        System.out.println("4");
        Club1.setText(tabString[0]);
        Club2.setText(tabString[1]);
        Club3.setText(tabString[2]);
        Club4.setText(tabString[3]);
        Club5.setText(tabString[4]);
        Club6.setText(tabString[5]);
        System.out.println("5");
        Club7.setText(tabString[6]);
        Club8.setText(tabString[7]);
        Club9.setText(tabString[8]);
        Club10.setText(tabString[9]);
        Club11.setText(tabString[10]);
        Club12.setText(tabString[11]);

        return null;
    }
}

您應該使用事件使視圖線程更新視圖:)再看一下文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM