我注意到我的应用程序有一个有趣的事情(我在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)
与Async相关的致命异常!但我不明白为什么 我的应用:
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)Async工作正常:
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。 拜托,任何人都可以帮助我解决这个问题。 谢谢!
P.S我在Manifest文件中添加了Internet权限 P.P.S我使用JSoup 1.7.1版(作为外部jar文件添加)
答案 0 :(得分:4)
我这样做是右键点击了项目 - &gt;构建路径 - &gt;添加外部档案
请撤消该操作,然后在项目中创建一个libs/
目录(作为src/
,[{1}}等的同行),如果您还没有该目录。然后,将JSoup JAR放在res/
目录中。这将把它添加到您的构建路径并将其内容包含在您的APK文件中。