我从Android应用程序开始,我遇到了一些问题。我的应用程序接受HTML页面的一部分,并在Textviews中放置此HTML的一些文本。错误是:“只有创建视图层次结构的原始线程才能触及其视图”。如果我不知道,这意味着我应该将“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;
}
}
答案 0 :(得分:0)
您应该使用事件来使视图线程更新视图:)再看一下文档。