嘿,我有一個問題,我的android應用程序。我試圖從給定的URL下載文本到可編輯框,但是當我運行應用程序,並擊中按鈕它突然停止working.I使用asynctask下載,還蝕告訴我,類DownloadTask不當地使用異步任務不工作
public void sendMessage(View view) throws IOException {
new DownloadTask().execute();
}
private class DownloadTask extends AsyncTask{
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
try {
EditText tf = (EditText) findViewById(R.id.editText1);
String kupa = tf.getText().toString();
Document doc;
doc = Jsoup.connect(kupa).get();
String title = doc.text();
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(title);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(result);
}
}
此外,我加入兩行代碼到我的onCreate方法
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
如果這有助於分鐘API爲10,目標16 歡呼傢伙
嘗試DownloadTask()執行((無效)爲空)。 –
由於不會在主(ui)線程中執行,因此您不能在後臺使用UI代碼。 tv.setText(標題); 同樣對於將來的問題,請包括來自LogCat的StackTrace,我相信它會拋出一個異常,清楚地表明您嘗試在非UI線程上運行UI代碼。 –