我注意到我的应用程序有一件有趣的事情(我在 AsyncTask 中使用 Jsoup)
之后我的应用程序崩溃了。LogCat 是:
12-15 11:16:43.023: I/dalvikvm(371): Could not find method org.jsoup.Jsoup.connect, referenced from method com.example.myexample.MainActivity$MyTask.doInBackground
12-15 11:16:43.033: W/dalvikvm(371): VFY: unable to resolve static method 3462: Lorg/jsoup/Jsoup;.connect (Ljava/lang/String;)Lorg/jsoup/Connection;
12-15 11:16:43.033: D/dalvikvm(371): VFY: replacing opcode 0x71 at 0x0003
12-15 11:16:43.033: D/dalvikvm(371): VFY: dead code 0x0006-000d in Lcom/example/myexample/MainActivity$MyTask;.doInBackground ([Ljava/lang/String;)Ljava/lang/String;
12-15 11:16:43.113: W/dalvikvm(371): threadid=9: thread exiting with uncaught exception (group=0x40015560)
12-15 11:16:43.133: E/AndroidRuntime(371): FATAL EXCEPTION: AsyncTask #1
12-15 11:16:43.133: E/AndroidRuntime(371): java.lang.RuntimeException: An error occured while executing doInBackground()
12-15 11:16:43.133: E/AndroidRuntime(371): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-15 11:16:43.133: E/AndroidRuntime(371): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-15 11:16:43.133: E/AndroidRuntime(371): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-15 11:16:43.133: E/AndroidRuntime(371): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-15 11:16:43.133: E/AndroidRuntime(371): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-15 11:16:43.133: E/AndroidRuntime(371): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-15 11:16:43.133: E/AndroidRuntime(371): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-15 11:16:43.133: E/AndroidRuntime(371): at java.lang.Thread.run(Thread.java:1019)
与异步有关的致命异常!但我不明白为什么我的应用程序:
public class MainActivity extends Activity {
MyTask mt;
TextView tvInfo;
String URL="http://en.wikipedia.org/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvInfo = (TextView) findViewById(R.id.tvInfo);
}
public void onclick(View v) {
mt = new MyTask();
mt.execute(URL);
}
class MyTask extends AsyncTask<String, Void, String> {
Document doc;
String title=null;
String what1=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
tvInfo.setText("Please wait...");
}
protected String doInBackground(String... params) {
// TimeUnit.SECONDS.sleep(2);
String url=params[0];
Document doc;
try {
doc = Jsoup.connect(url).get();
String what1=doc.title();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return what1;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvInfo.setText(result);
}
}
}
如果我更改我的代码(没有 JSoup)异步效果很好:
public class MainActivity extends Activity {
MyTask mt;
TextView tvInfo;
String URL="http://en.wikipedia.org/";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvInfo = (TextView) findViewById(R.id.tvInfo);
}
public void onclick(View v) {
mt = new MyTask();
mt.execute(URL);
}
class MyTask extends AsyncTask<String, Void, String> {
Document doc;
String title=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
tvInfo.setText("Please wait");
}
protected String doInBackground(String... params) {
// TimeUnit.SECONDS.sleep(2);
String url=params[0];
//doc = Jsoup.connect(url).get();
//String title = doc.title();
// Log.d("AsyncTask doInBackground","URL: " + params[0]);
return url;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvInfo.setText(result);
}
}
}
在 TextView 中,我得到了一个 Wiki 的 URL。拜托,任何人都可以帮助我解决这个问题。谢谢!
PS 我在 Manifest 文件中添加 Internet 权限 PPS 我使用 JSoup 版本 1.7.1(作为外部 jar 文件添加)