基本上,當我執行我的應用程序時,它會崩潰,或者不顯示任何文本或任何東西。 我使用的片段和viewPager在我的主要活動,但導致該問題的階級是這樣的一個:Jsoup和setText在android中不工作
public class DailyWord extends Fragment {
Document doc;
String data;
TextView t;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
View mainView = inflater.inflate(R.layout.activity_daily_word, container, false);
try {
doc = Jsoup.connect("http://www.urbandictionary.com/").get();
} catch (IOException e) {
e.printStackTrace();
}
Element Worddata = doc.getElementsByClass("word").first();
Element Worddata2 = Worddata.select("a[href]").first();
String wordName = Worddata2.text();
Element definition = doc.getElementsByClass("description").first();
String define = definition.text();
Element example = doc.getElementsByClass("example").first();
String context = example.text();
data = ("Word:\n" + wordName + "\n" + "Definition\n" + define + "\n" + "Context:\n" + context);
t = (TextView) mainView.findViewById(R.id.textView1);
t.setText(data);
return mainView;
}
}
後堆棧跟蹤。意識到這是一個NetworkOnMainThreadException。將問題重複爲1000人。 – njzk2
http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception –
做它喜歡這個鏈接的下載答案couldnt在這裏爲你回答它becos線程關閉.. http://wl.filegenie.com /~ikp4success/AnswerJsoup.txt – ImGeorge