简体   繁体   中英

SetText Impossible because bad thread

I'm beginning with android apps and I got a little problem. My app takes a part of a HTML Page and puts some text of this HTML in Textviews. The error is: "Only the original thread that created a view hierarchy can touch its views". If I undestood, it means I should put the "setText()" in the onCreate() function but it doesn't work neither because the HTML isn't completely loaded.

Sorry for my poor english and thank's if you have an answer.

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

您应该使用事件使视图线程更新视图:)再看一下文档。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM